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.
for loop, error in model frame.default ... variable lengths differ
5 messages · Peter Keller, Jeff Newmiller, PIKAL Petr +2 more
No data, not reproducible.
I think you should be using na.omit, though.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Peter Keller <kellerp.l at gmail.com> wrote:
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.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hi You did not provide data but I can see some problems in your code. See inline.
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)) {
So i is actually values from 1 to length of vars variable.
aggregate(colnames(df)[i] ~ x1 + x2 + x3, df, mean,
and you select variables from df[,1] to df[, length(vars)], which is probably not what you want. What is x1-x3? are they variables in df?
na.action=na.exclude)
for mean the correct statement is na.rm=TRUE
} I get this error: Error in model.frame.default(formula = colnames(df)[i] ~ x1 + x2 + : variable lengths differ (found for 'x1')
Maybe x1 has different length as df. What length(x1) and dim(df) tells you? Regards Petr
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.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
On May 21, 2012, at 10:25 , Petr PIKAL wrote:
Hi You did not provide data but I can see some problems in your code. See inline.
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)) {
So i is actually values from 1 to length of vars variable.
aggregate(colnames(df)[i] ~ x1 + x2 + x3, df, mean,
and you select variables from df[,1] to df[, length(vars)], which is probably not what you want. What is x1-x3? are they variables in df?
na.action=na.exclude)
for mean the correct statement is na.rm=TRUE
} I get this error: Error in model.frame.default(formula = colnames(df)[i] ~ x1 + x2 + : variable lengths differ (found for 'x1')
Maybe x1 has different length as df. What length(x1) and dim(df) tells you?
colnames(df)[i] is a character vector of length 1. This doesn't work any better than
aggregate(colnames(airquality)[1] ~ Month, airquality, mean, na.rm=T)
Error in model.frame.default(formula = colnames(airquality[1]) ~ Month, : variable lengths differ (found for 'Month') What the poster probably wanted was something in the vein of
nm <- colnames(airquality)[1] ff <- formula(bquote(.(as.name(nm))~Month)) aggregate(ff, airquality, mean, na.rm=T)
Month Ozone 1 5 23.61538 2 6 29.44444 3 7 59.11538 4 8 59.96154 5 9 31.44828
Regards Petr
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.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Peter Dalgaard, Professor Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
On Mon, May 21, 2012 at 2:00 AM, peter dalgaard <pdalgd at gmail.com> wrote:
[snip]
What the poster probably wanted was something in the vein of
nm <- colnames(airquality)[1] ff <- formula(bquote(.(as.name(nm))~Month)) aggregate(ff, airquality, mean, na.rm=T)
?Month ? ?Ozone 1 ? ? 5 23.61538 2 ? ? 6 29.44444 3 ? ? 7 59.11538 4 ? ? 8 59.96154 5 ? ? 9 31.44828
or perhaps to use an implicit loop (since looping seemed to be part of it all):
results <- lapply(c("Ozone", "Solar.R"), function(n) {
aggregate(. ~ Month, airquality[, c(n, "Month")], mean, na.rm = TRUE)
})
## print results
results
## untested code based on OPs original example
vars <- colnames(df)[c(10,12,16,18,20,21,24:29,45)]
results <- lapply(vars, function(n) {
aggregate(. ~ x1 + x2 + x3, df[, c(n, "x1", "x2", "x3")], mean, na.rm = TRUE)
})
## print results
results
Cheers,
Josh
-- Peter Dalgaard, Professor Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk ?Priv: PDalgd at gmail.com
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/