[R] Selecting data frame components by name - do you know a shorter way?

Marc Schwartz (via MN) mschwartz at mn.rr.com
Fri Jan 20 19:55:25 CET 2006


One of the distinct advantages of using subset() with a large number of
columns is that given it's support for standard vector indexing in the
'select' argument, you can do something like:

  subset(dframe, select = c(name1, name5:name235, name437))

Here, by using the syntax "name5:name235", you are selecting a large
number of sequential columns in the dataframe, without having to
enumerate each column name individually. Of course, you can specify
multiple such sequential groups as appropriate.

HTH,

Marc Schwartz

On Fri, 2006-01-20 at 13:26 -0500, roger bos wrote:
> One idea is to keep the variable names you want in a vector, say 'use' and
> and get the indices using the match function:
> 
> match(use, names(df)
> 
> where use <- c("item1","item2",...)
> 
> You can then subset the data frame as follows:
> 
> newdf <- df[, match(use, names(df)]
> 
> HTH,
> 
> Roger
> 
> 
> On 1/20/06, Chuck Cleland <ccleland at optonline.net> wrote:
> >
> > ?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




More information about the R-help mailing list