Re: [R] does column exist?

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Fri 20 May 2005 - 04:12:27 EST

On Thu, 19 May 2005 apjaworski@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@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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Fri May 20 04:30:57 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:55 EST