Home > database >  Two sample t-test in a dataframe using R
Two sample t-test in a dataframe using R

Time:09-21

I made a data as follows:

data<-data.frame(weight=c(78,89,90,45,57,89,67,56,78,50),
                 height=c(170,158,162,190,193,190,167,169,170,175),
                 bmi=c(23,24,35,19,25,29,30,34,32,31),
                 disease=c(0,1,0,0,0,0,1,1,0,0))

What I want to do is two sample t test. Two groups are divided by disease. So, I made the data into two groups like this:

y0<-subset(data,disease==0)
y1<-subset(data,disease==1)

I want to do t-test for all variables. I think I can do like this:

t.test(y0$weight,y1$weight)
t.test(y0$height,y1$height)
t.test(y0$bmi,y1$bmi)

However, in my actual data, there are so many variables so that I cannot write the code as above. I want to do t-test for all variables using the position of the column. My expectation is to get a nice table including all the t values and p-values so that I can easily convert into a csv file.

CodePudding user response:

Something like this?

results_T <- NULL
results_P <-  NULL
for (i in 1:(ncol(data)-1)){
  results_T[i] <- t.test(y0[,i],y1[,i])$statistic[[1]]
  results_P[i] <- t.test(y0[,i],y1[,i])$p.value[[1]]
}

Results_T.Test <- rbind(results_T,results_P)
colnames(Results_T.Test) <- colnames(data[,1:(ncol(data)-1)])

write.csv(Results_T.Test,"Path to export the DataFrame\\File Name.csv")
  • Related