[R] Confounded data frame column names

Thomas Lumley tlumley at u.washington.edu
Wed Oct 6 00:17:14 CEST 2004


On Tue, 5 Oct 2004, Booker, Andrew J wrote:

> This is probably a know problem (problem for me anyway) in R but I
> don't quite know what to search for in help archives. When I name a
> column "x11" in a data frame R thinks a column named "x1" exists. In
> my application I am trying to test for the existence of a column, then
> add it if it's not there. Here is a simple example:
>
>> temd <- data.frame(x11=c(0:10))
>> is.null(temd[["x1"]])
> [1] FALSE

Yes. R allows partial matching in this and many other contexts.  You could 
use
   "x1" %in% names(temd)
to test for the name without partial matching (probably more efficiently 
if the data frame is large)

 	-thomas




More information about the R-help mailing list