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. Thanks, Rado
myDF[! names(myDF) %in% not_wanted] if I understand you aright. E.g. library(MASS) hills[! names(hills) %in% "climb"] which can also be done by subset(hills, select=-climb) On Mon, 5 Jul 2004, Rado Bonk wrote:> 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?-- 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
Rado, If you have a data.frame df like: Col1 Col2 Col3 Col4 1 2 A B Then you should be able to do: df[,c("Col2","Col4")] to get the second and fourth columns. Sean On 7/5/04 8:53 AM, "Rado Bonk" <rado.bonk at jrc.it> wrote:> 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. > > > Thanks, > > Rado > > ______________________________________________ > R-help at 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 >
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 at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Rado Bonk > Sent: Monday, July 05, 2004 7:54 AM > To: r-help at 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.
hi, see colnames() simple use, good result. 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 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 was playing with that for a little bit using the vector syntax but got > several syntax errors. > > > Thanks, > > Rado > > ______________________________________________ > R-help at 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-- ------ Yves Magliulo <ym at climpact.com> Climatology research department, CLIMPACT Tel. : +33 (0) 1 55 07 85 77 Fax. : +33 (0) 1 55 07 85 79 Universite Pierre et Marie Curie Boite 101 - Tour 45 - 5eme etage - Couloir 45/46 4 place Jussieu, 75252 Paris CEDEX 05, France