[R] extract columns from a dataframe

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jul 5 16:23:54 CEST 2004


On 5 Jul 2004, Yves Magliulo wrote:

> see colnames() 
> simple use, good result.

Not really correct use, though.  A data frame has names for its columns,
and a matrix has colnames.

> ex: if df is your data.frame and toto = the column name you want to
> extract do:
> 
> df2<-df[,colnames(df)==toto)] #extract all toto column

df["toto"] is much easier (and you do need the quotes, I believe).  It
even works with a vector of names.

> Le lun 05/07/2004 à 14:53, Rado Bonk a écrit :
> > Dear R users,
> > 
> > I'm coming back to R after while. I have a data frame with 200 columns, 
> > each column has a name. How to extract all columns to a new dataset, but 
> > the specified (by names) ones?

I read that as meaning `all except the specified columns'.

> > I was playing with that for a little bit using the vector syntax but got 
> > several syntax errors.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list