[R] does column exist?

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu May 19 20:12:27 CEST 2005

On Thu, 19 May 2005 apjaworski at mmm.com wrote:

> There may be a simpler way, but this
> is.na(match("X", names(df)))
> should return TRUE if the column name exists and FALSE otherwise.

Mere syntactic sugar, but "X" %in% names(mydf) is self-documenting.

> How do I test if a data.frame has a column named X?
> exists(o) checks if the object exists or not, I want to test if a
> data.frame object (df) has a column names(X), something like:
> exists(df$X)

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

More information about the R-help mailing list