Skip to content
Prev 244727 / 398502 Next

Why do we have to turn factors into characters for various functions?

Hello Petr,

don't want to convince you. If you like the following:

x <- factor(1:4, labels=c("one", "two", "three", "four"))

y <- factor(3:5, labels=c("three", "four", "five"))

data.frame(character=c(as.character(x), as.character(y)), numeric=c(x, y))

   character numeric
1       one       1
2       two       2
3     three       3
4      four       4
5     three       1
6      four       2
7      five       3

For me the behaviour of character vectors is easier to follow and 
less errror prone.

cx <- c("one", "two", "three", "four")

cy <- c("three", "four", "five")

c(cx, cy)

[1] "one"   "two"   "three" "four"  "three" "four"  "five"
I agree with you regarding personal habits. It's not the features of 
factors. For me it's the rather inconsistent use in functions like 
c() or print().
If you print a factor, you see it's levels, but if you combine it 
using c(), you combine the famouse implementation specific underlying 
integer vector.

best regards,

Heinz
At 13.12.2010 08:50 +0100, Petr PIKAL wrote: