install.packages(“rmarkdown”)

# sets wd to the path on my computer; 
setwd("C:\\Users\\hmon1\\Desktop\\10B+Homework\\") #this is where you downloaded the HW1.csv file
# loads in data for the full population
pop<-read.csv("HW9.csv")
# sets the seed for the random number generator
set.seed(48183130)  #use your student ID instead of 12345678
# assigns a "random" sample of 20 from the population to 'data'
sample<-pop[sample(1:nrow(pop), 20, replace=FALSE),]
data <- data.frame(table(sample$G, sample$X))
colnames(data) <- c("G", "X", "f")
table <- subset(data, f != 0)
# creates the table to fill in your homework
table <- data[order(data$G),]
table <- subset(table, f != 0)
table
##    G  X f
## 1  A 10 1
## 3  A 11 1
## 5  A 12 3
## 7  A 13 3
## 9  A 14 1
## 13 A 16 2
## 10 B 14 3
## 12 B 15 2
## 14 B 16 1
## 16 B 17 1
## 18 B 19 1
## 20 B 20 1
# calculates group means
meanA <- mean(sample$X[sample$G == "A"])
meanB <- mean(sample$X[sample$G == "B"])
meanA
## [1] 12.90909
meanB
## [1] 16
#calculates observed difference
obs_diff <- meanA - meanB
obs_diff
## [1] -3.090909
# calculates pooled variance
SSA <- sum(((sample$X - meanA)^2)[sample$G == "A"])
N_A <- length(sample$X[sample$G == "A"])
SSB <- sum(((sample$X - meanB)^2)[sample$G == "B"])
N_B <- length(sample$X[sample$G == "B"])

pooled <- (SSA + SSB)/(N_A + N_B - 2)
pooled
## [1] 4.161616
# calculates standard error of the sampling distribution
SE <- sqrt(pooled*(1/N_A + 1/N_B))
SE
## [1] 0.9169136
# two sample t-test
t.test(sample$X~sample$G,var.equal=TRUE, paired=FALSE)
## 
##  Two Sample t-test
## 
## data:  sample$X by sample$G
## t = -3.371, df = 18, p-value = 0.003403
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -5.017273 -1.164545
## sample estimates:
## mean in group A mean in group B 
##        12.90909        16.00000