R_help Help
2009-Jun-23 03:17 UTC
[R] Smart way to check if a column with certain name exists in a data.frame
Hi, I have a matrix or data.frame (df). When I would like to check if a column named "x" exists, it becomes my habit to do the following: if (length(which(colnames(df)=="x"))>0) Is there any smarter way to do this? Thank you. adschai [[alternative HTML version deleted]]
Kingsford Jones
2009-Jun-23 03:27 UTC
[R] Smart way to check if a column with certain name exists in a data.frame
try: 'x' %in% colnames(df) hth, Kingsford On Mon, Jun 22, 2009 at 9:17 PM, R_help Help<rhelpacc at gmail.com> wrote:> Hi, > > I have a matrix or data.frame (df). When I would like to check if a column > named "x" exists, it becomes my habit to do the following: > > if (length(which(colnames(df)=="x"))>0) > > Is there any smarter way to do this? Thank you. > > adschai > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >