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

John Kane jrkrideau at yahoo.ca
Thu Jan 10 15:50:05 CET 2008


I don't know why this is happening but it has nothing
to do with a negative index
df[,-2] has not changed df.  
--- Dieter Menne <dieter.menne at menne-biomed.de> wrote:

> 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 at 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.
> 



      Looking for the perfect gift? Give the gift of Flickr!




More information about the R-help mailing list