What is wrong with this loop ? I am getting an error saying incorrect number of dimensions y[i,2] x <- as.data.frame(runif(2000, 12, 38)) z <-numeric(length(x)) y <- as.data.frame(z) for(i in 1:length(x)) { y <- ifelse(i < 500, as.data.frame(lowess(x[1:i,1], f=1/9)) , as.data.frame(lowess(x[(i-499):i,1], f=1/9))) z[i] <-y[i,2] } -- View this message in context: http://r.789695.n4.nabble.com/What-am-I-doing-wrong-with-this-loop-tp3332703p3332703.html Sent from the R help mailing list archive at Nabble.com.
The "y" object within the loop returns a list and you are trying to put the first row of the 2nd column of the list (which refers to a vector of length 1 to i) into the object z, which is vector. I am not sure at all what you are trying to end up with. Scott On Wednesday, March 2, 2011 at 9:19 PM, eric wrote:> What is wrong with this loop ? I am getting an error saying incorrect number > of dimensions y[i,2] > > x <- as.data.frame(runif(2000, 12, 38)) > z <-numeric(length(x)) > y <- as.data.frame(z) > for(i in 1:length(x)) { > y <- ifelse(i < 500, as.data.frame(lowess(x[1:i,1], f=1/9)) , > as.data.frame(lowess(x[(i-499):i,1], f=1/9))) > z[i] <-y[i,2] > } > > -- > View this message in context: http://r.789695.n4.nabble.com/What-am-I-doing-wrong-with-this-loop-tp3332703p3332703.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. >[[alternative HTML version deleted]]
Bill.Venables at csiro.au
2011-Mar-03 04:31 UTC
[R] What am I doing wrong with this loop ?
Here is a start> x <- as.data.frame(runif(2000, 12, 38)) > length(x)[1] 1> names(x)[1] "runif(2000, 12, 38)">Why are you turning x and y into data frames? It also looks as if you should be using if(...) ... else ... rather than ifelse(.,.,), too. You need to sort out a few issues, it seems. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of eric Sent: Thursday, 3 March 2011 1:19 PM To: r-help at r-project.org Subject: [R] What am I doing wrong with this loop ? What is wrong with this loop ? I am getting an error saying incorrect number of dimensions y[i,2] x <- as.data.frame(runif(2000, 12, 38)) z <-numeric(length(x)) y <- as.data.frame(z) for(i in 1:length(x)) { y <- ifelse(i < 500, as.data.frame(lowess(x[1:i,1], f=1/9)) , as.data.frame(lowess(x[(i-499):i,1], f=1/9))) z[i] <-y[i,2] } -- View this message in context: http://r.789695.n4.nabble.com/What-am-I-doing-wrong-with-this-loop-tp3332703p3332703.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.
Never mind Bill....got it. Always seems to happen this way. Can't figure something out. Post to the site and wham, 5 min later (after posting), it's all clear. Oh well, thanks for the tips -- View this message in context: http://r.789695.n4.nabble.com/What-am-I-doing-wrong-with-this-loop-tp3332703p3334609.html Sent from the R help mailing list archive at Nabble.com.