Re: [R] extract columns from a dataframe

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Tue 06 Jul 2004 - 00:23:54 EST


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@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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Tue Jul 06 00:27:30 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:54:41 EST