[R] column extraction by name ?

Marlene Mueller marlene.mueller at gmx.de
Wed Nov 12 14:22:00 CET 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[,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/




More information about the R-help mailing list