[R] does column exist?

2005-05-19 Thread Omar Lakkis
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)

__
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


Re: [R] does column exist?

2005-05-19 Thread Chuck Cleland
How about is.null(df$X)==FALSE ?
Omar Lakkis wrote:
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)
__
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

--
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 452-1424 (M, W, F)
fax: (917) 438-0894
__
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


Re: [R] does column exist?

2005-05-19 Thread apjaworski





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.

Cheers,

Andy

__
Andy Jaworski
518-1-01
Process Laboratory
3M Corporate Research Laboratory
-
E-mail: [EMAIL PROTECTED]
Tel:  (651) 733-6092
Fax:  (651) 736-3122


   
 Omar Lakkis   
 [EMAIL PROTECTED] 
 m To 
 Sent by:  r-help@stat.math.ethz.ch
 [EMAIL PROTECTED]  cc 
 at.math.ethz.ch   
   Subject 
   [R] does column exist?  
 05/19/2005 12:42  
 PM
   
   
 Please respond to 
Omar Lakkis
 [EMAIL PROTECTED] 
m 
   
   




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)

__
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

__
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


Re: [R] does column exist?

2005-05-19 Thread Spencer Graves
	  It depends on what Omar means by a column named X.  Consider the 
following:

 tstDF - data.frame(aa=1, bb=2)
 tstDF$a
[1] 1
 is.null(tstDF$a)
[1] FALSE
 is.na(match(a, names(tstDF)))
[1] TRUE
	  Does tstDF have a column named a?  It has a column named aa, 
which is accessed by tstDF$a;  is.null(tstDF$a) correctly identifies its 
presence, while is.na(match(a, names(tstDF))) correctly says that a 
is not the official name of any column of tstDF.

  spencer graves
[EMAIL PROTECTED] 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.
Cheers,
Andy
__
Andy Jaworski
518-1-01
Process Laboratory
3M Corporate Research Laboratory
-
E-mail: [EMAIL PROTECTED]
Tel:  (651) 733-6092
Fax:  (651) 736-3122
   
 Omar Lakkis   
 [EMAIL PROTECTED] 
 m To 
 Sent by:  r-help@stat.math.ethz.ch
 [EMAIL PROTECTED]  cc 
 at.math.ethz.ch   
   Subject 
   [R] does column exist?  
 05/19/2005 12:42  
 PM
   
   
 Please respond to 
Omar Lakkis
 [EMAIL PROTECTED] 
m 
   
   


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)
__
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
__
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
__
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


Re: [R] does column exist?

2005-05-19 Thread Prof Brian Ripley
On Thu, 19 May 2005 [EMAIL PROTECTED] 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,  [EMAIL PROTECTED]
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, UKFax:  +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