[R] Dynamically build variable names

Stefan Petersson stefan.petersson at inizio.se
Mon May 17 10:41:54 CEST 2010


I'm trying to dynamically build variable names to use on a list. Let's say I have a list like this one:

l <- list(V1_1=c(1,2,3), V1_2=c('One','Two','Three'))

And I succesfully build my variable name like this:

paste('l$', 'V1_1', sep='')

Why can't I just run a mean call with the pasted variable name?

mean(paste('l$', 'V1_1', sep=''))

So, my question is; How do one build variable names dynamically to be able to use them on an R list.

-----------------------------------------------------------------------
> sessionInfo()
R version 2.11.0 (2010-04-22) 
i486-pc-linux-gnu 

locale:
 [1] LC_CTYPE=en_US.utf8          LC_NUMERIC=C                
 [3] LC_TIME=en_US.utf8           LC_COLLATE=en_US.utf8       
 [5] LC_MONETARY=en_US.utf8       LC_MESSAGES=en_US.utf8      
 [7] LC_PAPER=en_US.utf8          LC_NAME=en_US.utf8          
 [9] LC_ADDRESS=en_US.utf8        LC_TELEPHONE=en_US.utf8     
[11] LC_MEASUREMENT=en_US.utf8    LC_IDENTIFICATION=en_US.utf8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] foreign_0.8-40

loaded via a namespace (and not attached):
[1] tools_2.11.0



More information about the R-help mailing list