[R] Dynamically build variable names
stefan.petersson at inizio.se
Mon May 17 11:08:25 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.
I found a solution. After three days of banging my head, it just took me three
minutes after I posted.
mean(unclass(eval(parse(text=paste('d$', 'V1_1', sep='')))))
I have no idea if this is considered 'good R coding'. It works, but I would
appreciate comments never the less...
More information about the R-help