Anova,用于循环应用功能 [英] Anova, for loop to apply function
问题描述
> str(set)
'data.frame':1000 obs。 6个变量:
$ ID:因子..
$ a:因子..
$:因子..
$ c:因子..
$ dat :num ..
$对比:Ord.factor ..
> X
[1]abc
$ b为(我在1:length(X)){
my = X [i]
f = as.formula(paste(dat〜contrasts *,paste (我的Error(ID /(contrasts)),sep =+)))
sum = summary(aov(f,data = set))
}
X可以是非常大的,所以想到的是一个apply函数而不是for-loop.Is可能在这种情况下?
我试过这个:
apply(
as .matrix(X)1,function(i){
summary(aov(as.formula(paste(dat〜contrasts *,
paste(i,Error(ID /(contrasts) ),sep =+))),data = set))
}
)
但是这没有意义。任何人都可以帮我吗?
<$样本数据
set < - data.frame(ID = 1:10,a = letters [1:10],b = LETTERS [1:10],c <= c $ c> =字母[10:1],
dat = runif(10),contrasts = ordered(rep(1:2,5)))
X < - letters [1:3]#a, b,c
sapply(X,function(my){
f < - as.formula(paste(dat〜contrasts *,my,+ Error(ID / ))))
summary(aov(f,data = set))
},simplify = FALSE)
注意使用简化= FALSE的sapply。使用lapply也可以,但是它不会为列表组件添加名字。
>str(set)
'data.frame': 1000 obs. of 6 variables:
$ ID : Factor ..
$ a : Factor ..
$ b: Factor ..
$ c: Factor ..
$ dat : num ..
$ contrasts : Ord.factor ..
>X
[1] "a" "b" "c"
for (i in 1 :length(X) ){
my=X[i]
f=as.formula(paste("dat~contrasts*", paste(my,"Error(ID/(contrasts))",sep="+")))
sum = summary( aov (f, data =set))
}
X can be very huge, so was thinking about an apply function instead of for-loop.Is it possible in this case??
I tried this:
apply(
as.matrix(X), 1, function(i){
summary(aov(as.formula(paste("dat~contrasts*",
paste(i, "Error(ID/(contrasts))", sep="+"))), data=set))
}
)
But this makes no sense. Can anyone help me?
This ought to do it:
# Sample data
set <- data.frame(ID=1:10, a=letters[1:10], b=LETTERS[1:10], c=letters[10:1],
dat=runif(10), contrasts=ordered(rep(1:2, 5)))
X <- letters[1:3] # a,b,c
sapply(X, function(my) {
f <- as.formula(paste("dat~contrasts*",my,"+Error(ID/(contrasts))"))
summary(aov(f, data=set))
}, simplify=FALSE)
Note the use of sapply with simplify=FALSE. Using lapply also works, but it doesn't add names to the list components.
这篇关于Anova,用于循环应用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!