我想對一堆變量應用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
as.formula的是,我不知道關鍵部分卻一下!謝謝! – Heisenberg