[R] produce variable on the fly

Ben Bolker bolker at ufl.edu
Tue Aug 12 17:22:09 CEST 2008


jimineep <jamesrperkins <at> hotmail.com> writes:

> 
> 
> Hi guys,
> 
> I want to create variable on the fly: for example
> 
> for (i in 1:10) {
>     cat(paste("VAR",i,sep=""))
> }
> Will print VAR1, VAR2 etc up to VAR10. However I want to make these into
> variables, and then give them a value, for example:
> 
> vect = c(10:20)
> 
> for (i in 1:10) {
>     cat(paste("VAR",i,sep="")) = vect[i]
> }
> 

  This is almost a candidate for a FAQ: see

http://wiki.r-project.org/rwiki/doku.php?id=tips:data-misc:create_var_names

for the answer ("assign") and some reasons why it might be better to use lists
instead.

  Ben Bolker



More information about the R-help mailing list