[Rd] as.character on list (was read.table with ":" in column names)

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Jan 20 14:36:10 CET 2006


Prof Brian Ripley <ripley at stats.ox.ac.uk> writes:


> > The thing is that as.character on a list will first coerce factors to
> > numeric, then numeric to character.
> 
> Nope.  It just coerces an INTSXP to a STRSXP.  as.character (and all
> other forms of coercion that I can think of quickly) ignores classes
> except when initially dispatching.

OK. I just meant that "de facto" it is like as.character(as.integer(f))
 
> Note that these examples are special cases:
> 
> > as.character(d[1:2,])
> [1] "c(1, 2)" "c(1, 2)"
> 
> may also be unexpected but follows from the general (undocumented, I
> dare say) rules.

and unlike as.character(as.integer(f)), so I do stand corrected....

> > I'm not sure whether there could be a rationale for it, but it isn't
> > S-PLUS compatible (not 6.2.1 anyway, which is the most recent one
> > that I have access to).
> 
> My S-PLUS deparses:
> 
> > l <- list(a=factor("x"),b=factor("y"))
> > as.character(l)
> [1] "structure(.Data = 1, .Label = \"x\", class = \"factor\")"
> [2] "structure(.Data = 1, .Label = \"y\", class = \"factor\")"
> 
> which seems no better (and probably worse).

Same here. Arguably, we deparse too, we just discard attributes first.
Both S-PLUS and R will do

> as.character(list(a=1:5,b=3))
[1] "c(1, 2, 3, 4, 5)" "3"


> The only other consistent option I can see is for all coercion methods
> to dispatch at each element of a recursive object, which I suspect
> introduces a considerable overhead for very little gain.

Then again maybe not, but it is one of those things which have the
potential to break things in unexpected places if you change it.
 
> One could perhaps argue for a data.frame method, since coercion
> operations on dataframes are rare and that is a case where people get
> factors where they wanted character columns.

Agreed.

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-devel mailing list