ryszard.czerminski@pharma.novartis.com
2003-Nov-12 12:50 UTC
[R] column extraction by name ?
I have a data frame (df) with colums x, y and z. e.g. df <- data.frame(x = sample(4), y = sample(4), z = sample(4)) I can extract column z by: df$z or df[3] I can also extract columns x,y by: df[1:2] or by df[-3]. Is it possible to extract x,y columns in a "symbolic" fashion i.e. by equivalent of df[-z] (which is illegal) ??? Or alternativeley, is there an equivalent of "index" function, which would return index of the column given name ? Ryszard
?match ?pmatch On Wed, 12 Nov 2003 ryszard.czerminski at pharma.novartis.com wrote:> I have a data frame (df) with colums x, y and z. > e.g. df <- data.frame(x = sample(4), y = sample(4), z = sample(4)) > I can extract column z by: df$z or df[3] > I can also extract columns x,y by: df[1:2] or by df[-3]. > > Is it possible to extract x,y columns in a "symbolic" fashion i.e. > by equivalent of df[-z] (which is illegal) ???df[-match("z", names(df))] df[!(names(df) %in% "z")] etc -- 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
ryszard.czerminski at pharma.novartis.com wrote:> I have a data frame (df) with colums x, y and z. > e.g. df <- data.frame(x = sample(4), y = sample(4), z = sample(4)) > I can extract column z by: df$z or df[3] > I can also extract columns x,y by: df[1:2] or by df[-3]. > > Is it possible to extract x,y columns in a "symbolic" fashion i.e. > by equivalent of df[-z] (which is illegal) ???df[,dimnames(df)[[2]] %in% c("x","y")] or df[,dimnames(df)[[2]] != "z"] should do this. Marlene -- PD Dr. Marlene M?ller Fraunhofer ITWM Kaiserslautern, Abt. Finanzmathematik mailto:Marlene.Mueller at itwm.fhg.de, Tel/Fax: +49 631 205 4189/4139 http://www.itwm.fhg.de/de/fm__employees__mueller/mueller/
At 07:50 12/11/2003 -0500, ryszard.czerminski at pharma.novartis.com wrote:>I have a data frame (df) with colums x, y and z. >e.g. df <- data.frame(x = sample(4), y = sample(4), z = sample(4)) >I can extract column z by: df$z or df[3] >I can also extract columns x,y by: df[1:2] or by df[-3]. > >Is it possible to extract x,y columns in a "symbolic" fashion i.e. >by equivalent of df[-z] (which is illegal) ??? > >Or alternativeley, is there an equivalent of "index" function, >which would return index of the column given name ?colindex=function(dataframe,columname){ return(which(dimnames(dataframe)[[2]]==columname)) } > x=data.frame(diag(3)) > colindex(x,"X2") [1] 2 Eric colindex(x,"X2") -------------------------------------------------- L'erreur est certes humaine, mais un vrai d?sastre n?cessite un ou deux ordinateurs. Citation anonyme -------------------------------------------------- Eric Lecoutre Informaticien/Statisticien Institut de Statistique / UCL TEL (+32)(0)10473050 lecoutre at stat.ucl.ac.be URL http://www.stat.ucl.ac.be/ISpersonnel/lecoutre
Uh oh! Time for my occasional reminder post to use subset(), as in subset(df, select = -z) for this case.> -----Original Message----- > From: ryszard.czerminski at pharma.novartis.com > [mailto:ryszard.czerminski at pharma.novartis.com] > Sent: 12 November 2003 12:51 > To: R-help list > Subject: [R] column extraction by name ? > > > Security Warning: > If you are not sure an attachment is safe to open please contact > Andy on x234. There are 0 attachments with this message. > ________________________________________________________________ > > I have a data frame (df) with colums x, y and z. > e.g. df <- data.frame(x = sample(4), y = sample(4), z = sample(4)) > I can extract column z by: df$z or df[3] > I can also extract columns x,y by: df[1:2] or by df[-3]. > > Is it possible to extract x,y columns in a "symbolic" fashion i.e. > by equivalent of df[-z] (which is illegal) ??? > > Or alternativeley, is there an equivalent of "index" function, > which would return index of the column given name ? > > Ryszard > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >Simon Fear Senior Statistician Syne qua non Ltd Tel: +44 (0) 1379 644449 Fax: +44 (0) 1379 644445 email: Simon.Fear at synequanon.com web: http://www.synequanon.com Number of attachments included with this message: 0 This message (and any associated files) is confidential and\...{{dropped}}