Dear R Users: I'm a STATA user converting to R, and I'd like to be to do the following. #Assign var_1 and var_2 a value 10->var1 20->var2 #Now I'd like to print the values of var_1 and var_2 by looping through var_1 and var_2 in such a manner: while(y<3){ print(var_y) y+1->y } In STATA, the "y" appended to " var_" is called the local variable and the code would look like this: while y<3 ( display var`y' y=y+1 ) Stata would understand that `y' is first 1 and then 2, and print out the values assigned to var_1 and var_2. Is there a way to do this in R? How would R users fram this questions even? What terminology would you use? Thanks! -- View this message in context: http://r.789695.n4.nabble.com/Printing-a-variable-in-a-loop-tp4634673.html Sent from the R help mailing list archive at Nabble.com.
Hi Kat, On Thu, Jun 28, 2012 at 8:22 AM, kat_the_great <knv4 at hotmail.com> wrote:> Dear R Users: > > I'm a STATA user converting to R, and I'd like to be to do the following. > #Assign var_1 and var_2 a value > 10->var1 > 20->var2 > > #Now I'd like to print the values of var_1 and var_2 by looping through > var_1 and var_2 in such a manner: > > while(y<3){ > ?print(var_y) > y+1->y > }The nearest you can get is while (y < 3) { print(.GlobalEnv[[paste("var", y, sep="")]]) y <- y + 1 } .GlobalEnv (a list, or strictly speaking an environment) contains all variables at the top-level of the REPL But this is not how we do it in R. 1. if you want to "display" the variable, just type it> var12. In Stata, you are working with one (tabular) data set at any time. In R, you can work with multiple data sets (R construct: dataframes) at the same time. For example using the builtin anscombe data set Stata: use anscombe di x1 y1 x2 y2 // display all di x1 y1 x2 y2 if _n <= 10 R: # data(anscombe) # optional anscombe[, c("x1", "y1", "x2", "y2")] # index by column anscombe[1:10, c("x1", "y1", "x2", "y2")] # index by row & column head(anscombe[, c("x1","y1","x2","y2")], n=10] # same as above Regards, Jon
Hello, Can't you just use vectors? Untested example: var[1] <- 10 var[2] <- 20 y <- 1 while(y < 3) { print(var[y]) y <- y+1 } Take care Oliver On Wed, Jun 27, 2012 at 8:22 PM, kat_the_great <knv4 at hotmail.com> wrote:> Dear R Users: > > I'm a STATA user converting to R, and I'd like to be to do the following. > #Assign var_1 and var_2 a value > 10->var1 > 20->var2 > > #Now I'd like to print the values of var_1 and var_2 by looping through > var_1 and var_2 in such a manner: > > while(y<3){ > ?print(var_y) > y+1->y > } > > In STATA, the "y" appended to " var_" is called the local variable and the > code would look like this: > > while y<3 ( > display var`y' > y=y+1 > ) > > Stata would understand that `y' is first 1 and then 2, and print out the > values assigned to var_1 and var_2. Is there a way to do this in R? How > would R users fram this questions even? What terminology would you use? > > Thanks! > > > -- > View this message in context: http://r.789695.n4.nabble.com/Printing-a-variable-in-a-loop-tp4634673.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.-- Oliver Ruebenacker, Bioinformatics and Network Analysis Consultant President and Founder of Knowomics (http://www.knowomics.com/wiki/Oliver_Ruebenacker) Consultant at Predictive Medicine (http://predmed.com/people/oliverruebenacker.html) SBPAX: Turning Bio Knowledge into Math Models (http://www.sbpax.org)
Thanks for your reply Jon. I need to actually do more than print the name of the variable (I just made the example simpler). I need to manipulate var_1, var_2 etc. but setting values of NA to 0. So as you said, "1. if you want to "display" the variable, just type it>var_1" But how do I do this in a loop where the number portion of var is the counter in the loop? Thanks! Kat -- View this message in context: http://r.789695.n4.nabble.com/Printing-a-variable-in-a-loop-tp4634673p4634754.html Sent from the R help mailing list archive at Nabble.com.
Hi> > Thanks for your reply Jon. > > I need to actually do more than print the name of the variable (I justmade> the example simpler). I need to manipulate var_1, var_2 etc. but setting > values of NA to 0.Why? R has pretty strong system for handling NAs. The only exception AFAIK is cumsum x<-1:10 sum(x) [1] 55 x[5]<-NA sum(x) [1] NA sum(x, na.rm=T) [1] 50 cumsum(x) [1] 1 3 6 10 NA NA NA NA NA NA> > So as you said, "1. if you want to "display" the variable, just type it> >var_1 > " > > But how do I do this in a loop where the number portion of var is the > counter in the loop?You probably shall get familiar with list concept which is another strong feature of R. You can easily subset lists either by *apply functions or in for cycle just by indexing.> > Thanks! > Kat > > -- > View this message in context: http://r.789695.n4.nabble.com/Printing-a- > variable-in-a-loop-tp4634673p4634754.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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.