Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply the same command to a list of variables

Tags:

r

apply

I want to apply t-tests on a bunch of variables. Below is some mock data

d <- data.frame(var1=rnorm(10), 
                var2=rnorm(10), 
                group=sample(c(0,1), 10, replace=TRUE))

# Is there a way to do this in some sort of loop?
with(d, t.test(var1~group))
with(d, t.test(var2~group))

# I tried this but the loop did not give a result!?
varnames <- c('var1', 'var2')
for (i in 1:2) {
  eval(substitute(with(d, t.test(variable~group)),
                  list(variable=as.name(varnames[i]))))  
}

Also, is it possible to extract the values from the t-test's result (e.g. two group means, p-value) so that the loop will produce a neat balance table across the variables? In other words, the end result I want is not a bunch of t-tests upon one another, but a table like this:

Varname   mean1   mean2   p-value
Var1        1.1    1.2     0.989
Var2        1.2    1.3     0.912
like image 572
Heisenberg Avatar asked Jan 12 '23 19:01

Heisenberg


1 Answers

You can use formula and lapply like this

set.seed(1)
d <- data.frame(var1 = rnorm(10), 
                var2 = rnorm(10), 
                group = sample(c(0, 1), 10, replace = TRUE))


varnames <- c("var1", "var2")
formulas <- paste(varnames, "group", sep = " ~ ")
res <- lapply(formulas, function(f) t.test(as.formula(f), data = d))
names(res) <- varnames

If you want to extract your table, you can proceed like this

t(sapply(res, function(x) c(x$estimate, pval = x$p.value)))
     mean in group 0 mean in group 1     pval
var1         0.61288        0.012034 0.098055
var2         0.46382        0.195100 0.702365
like image 162
dickoa Avatar answered Jan 15 '23 10:01

dickoa