[R] rbind.data.frame: character comverted to factor

Rolf Turner rolf at math.unb.ca
Wed Feb 12 17:32:03 CET 2003


Brian Ripley wrote:

> d1 <- data.frame(id=I(letters[1:20]), x = runif(20))
> d2 <- data.frame(id=I(paste(letters[1:20],letters[1:20],sep="")),
>                   x=rexp(20))
> d3 <- rbind(d1, d2)
> 
> which of course works!

Why ``of course''?  It seems to me that there is no ``of course''
about it.  It is completely counter-intuitive.  What appears
to be going on is that the I() operator NOT ONLY puts its argument
into the data frame ``as is'', but it ALSO tacks a class ``AsIs''
onto that argument which prevents it from being mucked around with
thereafter.

This is a neat trick, but is fairly mysterious --- and could have
intricate ramifications.  How can one discern all the impacts of
an object's having ``AsIs'' as a class?  (It would appear that
objects of any structure and class can ``inherit from'' AsIs.)

It would be highly preferable not to have to use the I() operator, or
the ``AsIs'' class at all.  I.e. to have character vectors stay
character vectors unless the user explicitly asks them to be
converted to factors.  However Splus introduced the contrary policy
years ago, and R is stuck with it for compatibility reasons.


					cheers,

						Rolf Turner
						rolf at math.unb.ca




More information about the R-help mailing list