

x<-rnorm(10, mean=10, sd=1)
y<-rnorm(20 , mean=11.2, sd=1)
group<-rep(c(0,1), c(10, 20))

dat<-data.frame(c(x,y), group)

tout<-t.test(x,y)
names(tout)
tobs<-t.test(x,y)$statistic
perm<-10000
tstar<-rep(NA, perm)
for (i in 1:perm){
	group<-sample(c(rep("x", length(x)), rep("y", length(y))))
	px<-c(x,y)[group=="x"]
	py<-c(x,y)[group=="y"]
	tstar[i]<-t.test(px, py)$statistic
}
plot(density(tstar), main="Permuted Null Distribution")
x<-seq(-4,4, by=0.01)
lines(x,dt(x, df=tout$parameter), lty=2)
pvalue<-mean(abs(tstar) >= abs(tobs))
pvalue



