[R] for loop, error in model frame.default ... variable lengths differ
Peter Keller
kellerp.l at gmail.com
Mon May 21 04:10:57 CEST 2012
I'm failing to get a for loop working. I'm sure it's something simple, and I
have found some posts relating to it, but I'm just not understanding why
this isn't working.
I have a data frame and would like to loop through specific column names,
using aggregate() within a for loop. There are NA's scattered throughout
the data frame and I'm thinking it has something to do with that, but I
haven't been able to fix it.
vars <- colnames(df)[c(10,12,16,18,20,21,24:29,45)]
for(i in 1:length(vars)) {
aggregate(colnames(df)[i] ~ x1 + x2 + x3, df, mean,
na.action=na.exclude)
}
I get this error:
Error in model.frame.default(formula = colnames(df)[i] ~ x1 + x2 + :
variable lengths differ (found for 'x1')
There are probably much better ways to do this, and I would be happy to get
suggestions, but mostly I would like to know why the code isn't working.
Thanks-
Peter
--
View this message in context: http://r.789695.n4.nabble.com/for-loop-error-in-model-frame-default-variable-lengths-differ-tp4630698.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list