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

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to