Michael Reinecke
2006-Jan-20 17:55 UTC
[R] Selecting data frame components by name - do you know a shorter way?
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]]
Douglas Grove
2006-Jan-20 18:00 UTC
[R] Selecting data frame components by name - do you know a shorter way?
So you want to create a subset of a data frame?
with components "name1" "name2" "name3" ...
dframe[, c("name1","name2","name3",...)]
will do that
Doug
On Fri, 20 Jan 2006, Michael Reinecke wrote:
> 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]]
>
>
Chuck Cleland
2006-Jan-20 18:02 UTC
[R] Selecting data frame components by name - do you know a shorter way?
?subset
subset(dframe, select=c("name1", "name2",
"name3"))
Michael Reinecke wrote:> 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 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
--
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 452-1424 (M, W, F)
fax: (917) 438-0894
Marc Schwartz (via MN)
2006-Jan-20 18:17 UTC
[R] Selecting data frame components by name - do you know a shorter way?
On Fri, 2006-01-20 at 18:55 +0100, Michael Reinecke wrote:> 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?> MichaelSee ?subset: subset(dframe, select = c(name1, name2, name3)) Alternatively, if the number of columns to remove is less than the number of columns to select, you can precede the column names with a "-" as per standard indexing conventions. See the Details section and the examples in ?subset. HTH, Marc Schwartz