2013-07-25 28 views
3

我想對一堆變量應用t檢驗。下面是一些模擬數據如何將相同的命令應用於變量列表

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])))) 
} 

此外,有可能從t-檢驗的結果(例如,兩個基團是指,p值)提取值,使得循環將橫跨變量產生一個整潔的平衡表?換句話說,最終的結果我想是不是一堆在彼此t檢驗,但像這樣的表:

Varname mean1 mean2 p-value 
Var1  1.1 1.2  0.989 
Var2  1.2 1.3  0.912 

回答

5

您可以使用formulalapply這樣

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 

如果要提取表,那麼您可以繼續像這樣

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 
+1

as.formula的是,我不知道關鍵部分卻一下!謝謝! – Heisenberg

2

這是一個重塑/ plyr解決方案: 的foo功能是主力,它運行t檢驗並提取手段和p值。

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

require(reshape2) 
require(plyr) 

dfm <- melt(d, id = 'group') 

foo <- function(x) { 
    tt <- t.test(value ~ group, data = x) 
    out <- data.frame(mean1 = tt$estimate[1], mean2 = tt$estimate[2], P = tt$p.value) 
    return(out) 
} 

ddply(dfm, .(variable), .fun=foo) 
# variable  mean1  mean2   P 
#1  var1 -0.2641942 0.3716034 0.4049852 
#2  var2 -0.9186919 -0.2749101 0.5949053 
0

使用sapply以t檢驗適用於所有varnames和子集化「估計」和「p.value」提取必要的數據。檢查names(with(d, t.test(var1~group)))如果要提取其他信息

t(with(d, sapply(varnames, function(x) unlist(t.test(get(x)~group)[c("estimate", "p.value")])))) 
相關問題