[R] How to convert a string to the column it represents in a dataframe, with a reproducible example

David Winsemius dwinsemius at comcast.net
Fri Jan 18 00:02:13 CET 2013


On Jan 17, 2013, at 2:26 PM, mtb954 at gmail.com wrote:

> Hi David,
> 
> I would like to have two objects, one containing the values in a column and the other containing the column's name. 

You have not addressed the question ... why? Where are you going with this?

> Of course, that's easy to do manually, but I don't want to have to type out the name of the column more than once (thus, below, I have typed it once in quotes, and I am trying to find a programatic way to create the other object, without typing the column name again).

I would think that this is be best way to proceed:

x <- cars[ , "dist",  drop=FALSE]

Now "x" is a data.frame (and inherits from the list-class)  and names(x) will return "dist" and the usual access methods would work. 

-- 
David.
> 
> Thank you for your help.
> 
> Mark Na
> 
> 
> On Thu, Jan 17, 2013 at 4:06 PM, David Winsemius <dwinsemius at comcast.net> wrote:
> 
> On Jan 17, 2013, at 1:36 PM, mtb954 at gmail.com wrote:
> 
> > Hello R-helpers,
> >
> > I have run the following lines of code:
> >
> > x<-"cars$dist"
> > y<-noquote(x)
> >
> >
> > Now y is a string containing the characters "cars$dist"
> >
> > My question....is there an R function (or combination of functions) that I
> > can apply to y that will cause y to contain the numbers in cars$dist? Even
> > better, can I do it without using noquote()?
> 
> What is the goal of this effort?
> 
> --
> 
> David Winsemius
> Alameda, CA, USA
> 
> 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list