[R] Variable names AS variable names?

Noah Silverman noah at smartmediacorp.com
Fri Feb 25 07:55:07 CET 2011


How can I dynamically use a variable as the name for another variable?

I realize this sounds cryptic, so an example is best:

#Start with an array of "codes"
codes <- c("a1", "b24", "q99")

#Each code has a corresponding matrix (could be vector)
a1 <- matrix(rnorm(100), nrow=10)
b24 <- matrix(rnorm(100), nrow=10)
q99 <- matrix(rnorm(100), nrow=10)

#Now, I want to loop through all the codes and do something with each matrix
for(code in codes){
    #here is where I'm stuck.  I don't want the value of code, but the
variable who's name is the value of code

}


Any suggestions?

-N



More information about the R-help mailing list