[R] column extraction by name ?

Eric Lecoutre lecoutre at stat.ucl.ac.be
Wed Nov 12 14:24:49 CET 2003


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




More information about the R-help mailing list