[R] How to retrieve a column name of a data frame

R. Michael Weylandt michael.weylandt at gmail.com
Thu Feb 2 17:36:00 CET 2012


I'd use something like

which(df == "b", arr.ind = TRUE)

which, gives the column number in the second spot; this gives you

colnames(df)[which(df == "b", arr.ind = TRUE)[2]]

Michael


On Thu, Feb 2, 2012 at 11:00 AM, ikuzar <razuki at hotmail.fr> wrote:
> Sorry, it was not clear:
>
> my program have to return column name  corresponding to a value, for example
> 'b' (so, the corresponding column is c1)
> How to retrieve c1 ?
> Thanks
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-retrieve-a-column-name-of-a-data-frame-tp4351764p4351866.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list