[R] referencing headers to extract components of a data frame

David Winsemius dwinsemius at comcast.net
Sat May 19 22:47:43 CEST 2012


On May 19, 2012, at 3:32 PM, Chris North wrote:

> Hi there,
>
> I'm working on a for loop to perform some simple calculations on data.
> However, I'm having trouble extracting components from my data frame  
> using
> names pulled off a list to reference headers. I'm relatively new to  
> R, but
> I've done some programming in various other languages, and have done  
> this
> kind of thing before. How is this done in R?
>
> Example code, to demonstrate what I'm attempting to do:
>
>> work_data$i.15.0
> [1] 0.03 0.03 0.03 0.02 0.02 0.03 0.02 0.02
>
>> currentFA
> [1] "i.15.0"
>
>> work_data$currentFA   *this is what I want to do, access my data  
>> frame by
> calling with header name pulled from elsewhere.
> NULL
>
>> curFA <- noquote(currentFA)   *tried stripping quotes, still  
>> doesn't work
>> curFA
> [1] i.15.0
>
>> work_data$curFA

The '$' function does not evaluate its argument but '[[' does. I do  
not see a complete example here, but try:

work_data[[currentFA]]

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list