[R] rbind.data.frame: character comverted to factor
Prof Brian D Ripley
ripley at stats.ox.ac.uk
Wed Feb 12 16:14:02 CET 2003
Read ?data.frame: that tells you to use
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!
On 12 Feb 2003, Markus [ISO-8859-1] Jäntti wrote:
> Dear All,
>
> on rbind:ing together a number of data.frames, I found that
> character variables are converted into factors. Since this
> occurred for a data identifier, it was a little inconvenient
> and, to me, unexpected. (The help page explains the
> general procedure used. I also found that on forming
> a data frame, character variables are converted to factors.
as documented in many places, including ?data.frame.
>
> The help page on read.table has the 'as.is' argument, which
> I suppose kind of suggests that character variables tend to
> get converted into factors. Is there such a "preference" for
> factors and should this behaviour be expected?
It's as documented.
> Example code
>
> d1 <- data.frame(id =letters[1:20], x = runif(20))
> d2 <- data.frame(id =paste(letters[1:20],letters[1:20], sep = ""), x =
> rexp(20))
> d3 <- rbind(d1, d2)
> str(d1) # <- id is factor
> str(d2) # <- id is factor
> str(d3) # <- id is factor
> d1[["id"]] <- as.character(d1[["id"]])
> d2[["id"]] <- as.character(d2[["id"]])
> d3 <- rbind(d1, d2)
> str(d1) # <- id is character
> str(d2) # <- id is character
> str(d3) # <- id is factor
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list