[R] rbind.data.frame: character comverted to factor
Markus Jäntti
markus.jantti at iki.fi
Wed Feb 12 16:02:06 CET 2003
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.
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?
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
Regards,
Markus
--
Markus Jäntti <markus.jantti at iki.fi>
Statistics Finland
More information about the R-help
mailing list