[R] Printing a variable in a loop

Petr PIKAL petr.pikal at precheza.cz
Thu Jun 28 18:07:41 CEST 2012


Hi
> 
> 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. 

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 guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list