Skip to content
Back to formatted view

Raw Message

Message-ID: <Pine.LNX.4.61.0505191909480.13182@gannet.stats>
Date: 2005-05-19T18:12:27Z
From: Brian Ripley
Subject: does column exist?
In-Reply-To: <OFFB688EFD.2F771D67-ON86257006.00621F35-86257006.00624612@mmm.com>

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