[R] accessing list components with a variable

Jeffrey J. Hallman jhallman at frb.gov
Mon Jul 16 15:26:06 CEST 2007


the_list[[comp]]

"David C. James" <dj at davidcjames.com> writes:

> Let's say I have a list called the_list consisting of three components:
> the_list$component_1
> the_list$component_2
> the_list$component_3
> 
> Now, I want to access it using a variable called comp.
> comp <- "component_1"
> 
> I'm looking for some function that let's me do this:
> unknown_function(the_list, comp)
> 
> Which should do the same thing as:
> the_list$component_1
> 
> Any ideas?  I'd be open to other ways of doing this, too.  I explored  
> this way, but it didn't seem to get me anywhere:
> the_list$"component_1"
> 
> I possibly could have gotten further along if I knew how to do  
> evaluation in R.
> 
> Thanks,
> David
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 

-- 
Jeff



More information about the R-help mailing list