[R] `[.data.frame`(df3, , -2) and NA columns

Dieter Menne dieter.menne at menne-biomed.de
Thu Jan 10 12:00:19 CET 2008


Dear baseRs,

I recently made a mistake when renaming data frame columns, accidentally
creating an NA column. I found the following strange behavior when negative
indexes are used.

Can anyone explain what happens here.  No "workarounds" required, just  curious.

Dieter

Version: Windows, R version 2.6.1 (2007-11-26)

#-----------------------------
df = data.frame(a=0:10,b=10:20)
df[,-2] #ok
names(df)=c("A") # implicitly creates an NA column
df[,-2] 
df[,-2,drop=FALSE]       # has nothing to do with drop

df3 = data.frame(a=0:10,b=10:20,c=20:30)
df3[,-2] #ok
names(df3)=c("A","B") #creates an NA column
df3[,-2] # error
# Error in `[.data.frame`(df3, , -2) : undefined columns selected

names(df3)[3]="NaN" # another reserved word
df3[,-2] # no problem




More information about the R-help mailing list