[Rd] setdiff bizarre (was: odd behavior out of setdiff)

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Jun 2 19:23:00 CEST 2009


On Tue, Jun 2, 2009 at 4:13 PM, Stavros Macrakis <macrakis at alum.mit.edu> wrote:

> but simply treats the data frame as a *character* list:
>
>     1 %in% data.frame(a=2,b=1)  # TRUE
>     '1' %in% data.frame(a=2,b=1)  # TRUE
>     1 %in% data.frame(a=2:3,b=1:2) # FALSE
>     1:3 %in% data.frame(a=2:4,b=1:3)  # FALSE FALSE FALSE
>     '1:3' %in% data.frame(a=2:4,b=1:3)  # TRUE

It applies as.character to the dataframe:

 > z=data.frame(a=2:4,b=1:3)
 > as.character(z)
 [1] "2:4" "1:3"

  The as.character method for data frames seems to spot integer
sequences (but only for int types and not num types) and show the a:b
notation:

 > x=data.frame(z=as.integer(c(1,2,3,4,5)))
 > str(x)
 'data.frame':	5 obs. of  1 variable:
  $ z: int  1 2 3 4 5
 > as.character(x)
 [1] "1:5"

 Obviously it doesn't do this for vectors:

 > as.character(x$z)
 [1] "1" "2" "3" "4" "5"

 I suspect it's using 'deparse()' to get the character representation.
This function is mentioned in ?as.character, but as.character.default
disappears into the infernal .Internal and I don't have time to chase
source code - it's sunny outside!

Barry



More information about the R-devel mailing list