[R] Printing a variable in a loop

Oliver Ruebenacker curoli at gmail.com
Thu Jun 28 12:25:51 CEST 2012


     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)



More information about the R-help mailing list