Chris North
2012-May-19 19:32 UTC
[R] referencing headers to extract components of a data frame
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 bycalling with header name pulled from elsewhere. NULL> curFA <- noquote(currentFA) *tried stripping quotes, still doesn't work > curFA[1] i.15.0> work_data$curFANULL Thanks! Chris [[alternative HTML version deleted]]
David Winsemius
2012-May-19 20:47 UTC
[R] referencing headers to extract components of a data frame
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$curFAThe '$' 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