RE: [R] extract columns from a dataframe

From: John Fox <jfox_at_mcmaster.ca>
Date: Mon 05 Jul 2004 - 23:05:06 EST


Dear Rado,

If the data frame is named df and nms is a vector names of the columns that you want to exclude, then

df[,-sapply(nms, function(x) which(x == names(df)))]

Should give you what you want.

I hope that this helps,
 John

> -----Original Message-----
> From: r-help-bounces@stat.math.ethz.ch
> [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Rado Bonk
> Sent: Monday, July 05, 2004 7:54 AM
> To: r-help@stat.math.ethz.ch
> Subject: [R] extract columns from a dataframe
>
> 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 was playing with that for a little bit using the vector
> syntax but got several syntax errors.



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 Mon Jul 05 23:08:19 2004

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