Re: [R] for loop, error in model frame.default ... variable lengths differ
On Mon, May 21, 2012 at 2:00 AM, peter dalgaard 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.4 > 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@cbs.dk Priv: pda...@gmail.com > > __ > R-help@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/ __ R-help@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.
Re: [R] for loop, error in model frame.default ... variable lengths differ
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) MonthOzone 1 5 23.61538 2 6 29.4 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@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@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@cbs.dk Priv: pda...@gmail.com __ R-help@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.
Re: [R] for loop, error in model frame.default ... variable lengths differ
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@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@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.
Re: [R] for loop, error in model frame.default ... variable lengths differ
No data, not reproducible. I think you should be using na.omit, though. --- Jeff NewmillerThe . . Go Live... DCN:Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Peter Keller 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@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@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] for loop, error in model frame.default ... variable lengths differ
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@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.
Re: [R] For Loop Error
On Sat, Jan 28, 2012 at 10:25:47AM -0800, Melrose2012 wrote: > Hi Again, > > I am writing a 'for loop' to create a matrix of randomly sampled colors. > I've written this loop in matlab and it works fine. I then tried to do it > in R and apparently there is something wrong with my syntax b/c every time I > run the script, the for loop "blows up" at a different point in the loop. > > As you can see, I ask R to clear my workspace each time, so there shouldn't > be any variables in my workspace that are messing this up. > > rm(list=ls()) > > # Sample Size > size <- 53 > # Probability of each color based on company > P.company <- c(.14,.14,.16,.13,.20,.24) > # Names of colors > color <- c('Br','Y','G','R','O','Bl') > # Make an empty matrix that will be filled in by for loop > table.combos <- matrix(nrow = 1, ncol = 6); > > # For loop will run through choosing a random bag of 53 M&Ms 1 times > # and create a table enumerating the number of each color in each of these > 1 bags > for(i in 1:1) { > combos <- sample(color,size, replace = TRUE, prob = P.company) > table.combos[i, ] <- table(combos) > colnames(table.combos)<-c("Br","Y","G","R","O","Bl") > } > > Every time the loop blows up, I get back this error: > > Error in table.combos[i, ] <- table(combos) : > number of items to replace is not a multiple of replacement length Hi. Rui Barradas already pointed out that the problem is with samples, which do not contain all colors. Forcing table() to produce zero frequencies of colors, which do not occur, a factor may be used. Try, for example size <- 53 P.company <- c(.14,.14,.16,.13,.20,.24) color <- c('Br','Y','G','R','O','Bl') table.combos <- matrix(nrow = 1, ncol = 6); colnames(table.combos) <- color for(i in 1:1) { combos <- sample(color,size, replace = TRUE, prob = P.company) table.combos[i, ] <- table(factor(combos, levels=color)) } which(table.combos==0, arr.ind=TRUE) row col [1,] 588 1 [2,] 3005 1 [3,] 4535 1 [4,] 8314 1 [5,] 7654 2 [6,] 8607 3 [7,] 1790 4 [8,] 1980 4 [9,] 2991 4 [10,] 4550 4 [11,] 5868 4 [12,] 6704 4 [13,] 8769 4 [14,] 8823 4 [15,] 7861 5 Hope this helps. Petr Savicky. __ R-help@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.
Re: [R] For Loop Error
Hello, > > Every time the loop blows up, I get back this error: > > Error in table.combos[i, ] <- table(combos) : > number of items to replace is not a multiple of replacement length > This is because not all colors are present in that one sample that breaks the code. > > There is no apparent consistency on which "i" in the loop I get this > error. > There shouldn't be, every time you run the loop, 'sample' will take different values. To get reproducibility you would need 'set.seed'. A solution could be based on the following. It's important to sort, like the difference between 'tc1' and 'tc2' proves. It's 'tc2' you want. Note that I've reduced the sample size to 10. Note also that I initialize the matrices to zeros, not NA's. color <- sort(c('Br','Y','G','R','O','Bl')) tc1 <- matrix(0, nrow = 1, ncol = 6) tc2 <- matrix(0, nrow = 1, ncol = 6) colnames(tc1) <- color colnames(tc2) <- color set.seed(123) for(i in 1) { combos <- sample(color, 10, replace = TRUE, prob = P.company) tc1[i, unique(combos)] <- table(combos) tc2[i, sort(unique(combos))] <- table(combos) } table(combos) tc1 tc2 I hope this helps. Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/For-Loop-Error-tp4336585p4337469.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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] For Loop Error
Hi Again, I am writing a 'for loop' to create a matrix of randomly sampled colors. I've written this loop in matlab and it works fine. I then tried to do it in R and apparently there is something wrong with my syntax b/c every time I run the script, the for loop "blows up" at a different point in the loop. As you can see, I ask R to clear my workspace each time, so there shouldn't be any variables in my workspace that are messing this up. rm(list=ls()) # Sample Size size <- 53 # Probability of each color based on company P.company <- c(.14,.14,.16,.13,.20,.24) # Names of colors color <- c('Br','Y','G','R','O','Bl') # Make an empty matrix that will be filled in by for loop table.combos <- matrix(nrow = 1, ncol = 6); # For loop will run through choosing a random bag of 53 M&Ms 1 times # and create a table enumerating the number of each color in each of these 1 bags for(i in 1:1) { combos <- sample(color,size, replace = TRUE, prob = P.company) table.combos[i, ] <- table(combos) colnames(table.combos)<-c("Br","Y","G","R","O","Bl") } Every time the loop blows up, I get back this error: Error in table.combos[i, ] <- table(combos) : number of items to replace is not a multiple of replacement length There is no apparent consistency on which "i" in the loop I get this error. Sometimes i = 10, sometimes i = 685, sometimes i = 1954, sometimes i = 59, etc. If anyone can please tell me what I am doing wrong, it would be greatly appreciated! Cheers, Melissa -- View this message in context: http://r.789695.n4.nabble.com/For-Loop-Error-tp4336585p4336585.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.