Multiple "One-Sample t-test" in R -


i have data.frame, similar one:

cb <- data.frame(group = ("a", "b", "c", "d", "e"), wc = runif(100, 0, 100), ana = runif(100, 0, 100), clo = runif(100, 0, 100)) 

structure of actual dataframe:

str(cb) data.frame: 66936 obs of 89 variables:  $group: factor w/ 5 levels "a", "b", "c" ... $wc: int 19 28 35 92 10 23... $ana: num 17.2 48 35.4 84.2 $ clo: num 37.2 12.1 45.4 38.9 ....  mean <- colmeans(cb[,2:89]) mean wc     ana    clo    ... 52.45  37.23  50.12  ... 

i want perform 1 sample t.tests on every group , every variable

for did following:

a <- subset(cb, cb$group == "a") b <- subset(cb, cb$group == "b") ...  t_a_wc <- t.test(a$wc, mu = mean[1], alternative = "two.sided") t_b_wc <- t.test(b$wc, mu = mean[1], alternative = "two.sided") ....  t_a_ana <- t.test(a$ana, mu = mean[2], alternative = "two.sided") t_b_ana <- t.test(b$ana, mu = mean[2], alternative = "two.sided") ....  t_a_clo <- t.test(a$clo, mu = mean[3], alternative = "two.sided") t_b_clo <- t.test(b$clo, mu = mean[3], alternative = "two.sided") .... 

the results correct (or seem be), time consuming typing whole thing many times.

is there smarter way that?

what have tried:

from here

results <- lapply(mydf, t.test) resultsmatrix <- do.call(cbind, results) resultsmatrix[c("statistic","estimate","p.value"),] 

but results somehow wrong , not fit values calculated priorly.

edit:

here link 10.000 row sample actual dataset

first, let's initialise results matrix , group levels.

res <- matrix(na, ncol=5,      dimnames=list(null, c("group", "col", "statistic", "estimate", "p.value"))) gr <- levels(cb$group) 

then loop through columns calculate t.test, subsetting each every available group.

for(cl in 2:ncol(cb)){     for(grp in gr){         temp <- cb[cb$group == grp, cl]         res <- rbind(res, c(grp, colnames(cb)[cl],              unlist(t.test(temp, mu = mean(cb[,cl]), alternative="two.sided"))[c(1, 5, 3)]))     } } 

and finally, reformat results table.

res <- data.frame(res[-1,]) 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -