r - Anova, for loop to apply function -



r - Anova, for loop to apply function -

>str(set) 'data.frame': 1000 obs. of 6 variables: $ id : factor .. $ : factor .. $ b: factor .. $ c: factor .. $ dat : num .. $ contrasts : ord.factor .. >x [1] "a" "b" "c" (i in 1 :length(x) ){ my=x[i] f=as.formula(paste("dat~contrasts*", paste(my,"error(id/(contrasts))",sep="+"))) sum = summary( aov (f, info =set)) }

x can huge, thinking apply function instead of for-loop.is possible in 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 makes no sense. can help me?

this ought it:

# sample info 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 utilize of sapply simplify=false. using lapply works, doesn't add together names list components.

r loops for-loop apply anova

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -