Scotty Nelson
2008-Oct-21 06:01 UTC
[R] For loop - how to assign "i" when it is not an element of an index?
Hello, I'm trying to build a for loop, where I estimate a series of models with different sets of (time series) data. However my for loop doesn't recognize the "i" ##################### code################ window.1=anomalies.CAK[(positions(anomalies.CAK)>=timeDate("1/1/1971") & positions(anomalies.CAK)<=timeDate("6/30/1991") )] .... window.14=anomalies.CAK[(positions(anomalies.CAK)>=timeDate("1/1/1984") & positions(anomalies.CAK)<=timeDate("6/30/2004") ),] for (i in 1:14){ ar1mods[1] = lm ( formula = window.i~ ar(1) ) } ################################# end code ################### Problem: Object "window.i" not found Anybody have a quick tip on what I'm doing wrong? -- View this message in context: http://www.nabble.com/For-loop---how-to-assign-%22i%22-when-it-is-not-an-element-of-an-index--tp20083917p20083917.html Sent from the R help mailing list archive at Nabble.com.
Dieter Menne
2008-Oct-21 06:14 UTC
[R] For loop - how to assign "i" when it is not an element of an index?
Scotty Nelson <poorboy44 <at> hotmail.com> writes:> I'm trying to build a for loop, where I estimate a series of models with > different sets of (time series) data. > However my for loop doesn't recognize the "i" > > ##################### code################ > window.1=anomalies.CAK[(positions(anomalies.CAK)>=timeDate("1/1/1971") & > positions(anomalies.CAK)<=timeDate("6/30/1991") )] > .... > window.14=anomalies.CAK[(positions(anomalies.CAK)>=timeDate("1/1/1984") & > positions(anomalies.CAK)<=timeDate("6/30/2004") ),] > > for (i in 1:14){ > ar1mods[1] = lm ( formula = window.i~ ar(1) ) > }The easiest would be to put the selection of the cases into the loop, and always use the same formula. You could also use formula=paste("window.",i,sep=""). That approach will work for the simple example, but you might run into eval-problems later (though probably not for lm). # B.D. Ripley val(subst) workaround #http://finzi.psych.upenn.edu/R/Rhelp02a/archive/16599.html Dieter
Bert Gunter
2008-Oct-21 15:28 UTC
[R] For loop - how to assign "i" when it is not an element of an index?
1. Thanks for the reproducible example. It makes the issue clear. 2. Your error reflects a profound confusion about how programming works in general, and R in particular. I think the following is a correct technical explanation (I am not a trained programmer, so corrections from those who are would be appreciated). R is not a macro language. The "i" in "for( i in 1:14)" is a name of a variable, not a symbol for which substitution will occur. window.i is also a name that **has nothing to do with the variable named "i" ** . To do what you want, you need to explcitly make the connection. One could do it the way you have set things up by first composing the names of the window.i objects and then associating the names with the objects: ... for (i in 1:14){ ... window.i <- get(paste("window","i",sep=".")) ... } But this construction is clumsy. A better way to do things is to make your windows and armods objects different components of a list and refer to them via subscripting: windows <- as.list(1:14) armods <- windows windows[[1]] <- ... windows[[2]] <- ... ... ## warning: untested for(i in 1:14){ ... armods[[i]] <- lm ( formula = window[[i]] ~ ar(1) ) ... } Use of a programming language -- any language -- requires a learning commitment that you do not appear to have made. Please spend some quality time with "An Introduction to R" and other R learning resources before you post to the list again. -- Bert Gunter -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Scotty Nelson Sent: Monday, October 20, 2008 11:02 PM To: r-help at r-project.org Subject: [R] For loop - how to assign "i" when it is not an element of an index? Hello, I'm trying to build a for loop, where I estimate a series of models with different sets of (time series) data. However my for loop doesn't recognize the "i" ##################### code################ window.1=anomalies.CAK[(positions(anomalies.CAK)>=timeDate("1/1/1971") & positions(anomalies.CAK)<=timeDate("6/30/1991") )] .... window.14=anomalies.CAK[(positions(anomalies.CAK)>=timeDate("1/1/1984") & positions(anomalies.CAK)<=timeDate("6/30/2004") ),] for (i in 1:14){ ar1mods[1] = lm ( formula = window.i~ ar(1) ) } ################################# end code ################### Problem: Object "window.i" not found Anybody have a quick tip on what I'm doing wrong? -- View this message in context: http://www.nabble.com/For-loop---how-to-assign-%22i%22-when-it-is-not-an-ele ment-of-an-index--tp20083917p20083917.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.