tags:

views:

55

answers:

2

I was surprised to see that R will coerce factors into a number when concatenating vectors. This happens even when the levels are the same. For example:

> facs <- as.factor(c("i", "want", "to", "be", "a", "factor", "not", "an", "integer"))
> facs
[1] i       want    to      be      a       factor  not     an      integer
Levels: a an be factor i integer not to want
> c(facs[1 : 3], facs[4 : 5])
[1] 5 9 8 3 1

what is the idiomatic way to do this in R (in my case these vectors can be pretty large)? Thank you.

+2  A: 

Wow, I never realized it did that. Here is a work-around:

x <- c(facs[1 : 3], facs[4 : 5]) 
x <- factor(x, levels=1:nlevels(facs), labels=levels(facs))
x

With the output:

[1] i    want to   be   a   
Levels: a an be factor i integer not to want

It will only work if the two vectors have the same levels as here.

Aniko
Great thanks! I've just figured out that unlist(list(facs[1 : 3], facs[4 : 5])) also works which is nice if you don't know ahead of time that facs is a factor type.
Keith
+1  A: 

An alternate workaround is to convert the factor to be a character vector, then convert back when you are finshed concatenating.

cfacs <- as.character(facs)
x <- c(cfacs[1:3], cfacs[4:5]) 

# Now choose between
factor(x)
# and
factor(x, levels = levels(facs))
Richie Cotton