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

From: Barry Rowlingson <b.rowlingson_at_lancaster.ac.uk>
Date: Tue, 02 Jun 2009 18:23:00 +0100

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



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 02 Jun 2009 - 17:36:31 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 02 Jun 2009 - 19:34:49 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive