Re: [R] Selecting data frame components by name - do you know a s horter way?

From: Liaw, Andy <andy_liaw_at_merck.com>
Date: Sat 21 Jan 2006 - 05:33:46 EST


Try:

dframe[c("name1", "name2", "name3")]

This works because a data frame is a list, with each variable the component of the list (and corresponding name), so you can subset it (by variables, not rows) like a list.

Andy

> From: Michael Reinecke
>
> Hi! I suspect there must be an easy way to access components
> of a data frame by name, i.e. the input should look like
> "name1 name2 name3 ..." and the output be a data frame of
> those components with the corresponding names. I īve been
> trying for hours, but only found the long way to do it (which
> is not feasible, since I have lots of components to select):
>
>
>
> dframe[names(dframe)=="name1" | dframe=="name2" | dframe=="name3"]
>
>
>
> Do you know a shortcut?
>
>
>
> Michael
>
>
> [[alternative HTML version deleted]]
>
>



R-help@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 Received on Sat Jan 21 05:45:13 2006

This archive was generated by hypermail 2.1.8 : Sat 21 Jan 2006 - 10:23:49 EST