46

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.

Rob
  • 25,569
  • 15
  • 73
  • 87
Keith
  • 2,686
  • 4
  • 25
  • 38

8 Answers8

38

From the R Mailing list:

unlist(list(facs[1 : 3], facs[4 : 5]))

To 'cbind' factors, do

data.frame(facs[1 : 3], facs[4 : 5])
fgregg
  • 2,945
  • 27
  • 36
10

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
  • 107,354
  • 40
  • 225
  • 343
8

Use fct_c from the forcats package (part of the tidyverse).

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

fct_c isn't fooled by concatenations of factors with discrepant numerical codings:

> x <- as.factor(c('c', 'z'))
> x
[1] c z
Levels: c z
> y <- as.factor(c('a', 'b', 'z'))
> y
[1] a b z
Levels: a b z
> c(x, y)
[1] 1 2 1 2 3
> fct_c(x, y)
[1] c z a b z
Levels: c z a b
> as.numeric(fct_c(x, y))
[1] 1 2 3 4 2
Connor Harris
  • 401
  • 4
  • 14
6

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
  • 16,905
  • 4
  • 43
  • 44
  • 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 Aug 09 '10 at 20:18
  • Setting levels manually in this way didn't work for my particular problem. (I have 0-based levels. I could have subtracted 1 and then reconstructed the factor, but, that is brittle and on the lesser end of the scrutabality spectrum, even for R.) Instead (hooray?) I went with `unlist(list(...))`. – David J. Apr 01 '13 at 04:50
4

This is a really bad R gotcha. Along those lines, here's one that just swallowed several hours of my time.

x <- factor(c("Yes","Yes","No", "No", "Yes", "No"))
y <- c("Yes", x)

> y
[1] "Yes" "2"   "2"   "1"   "1"   "2"   "1"  
> is.factor(y)
[1] FALSE

It appears to me the better fix is Richie's, which coerces to character.

> y <- c("Yes", as.character(x))
> y
[1] "Yes" "Yes" "Yes" "No"  "No"  "Yes" "No" 
> y <- as.factor(y)
> y
[1] Yes Yes Yes No  No  Yes No 
Levels: No Yes

As long as you get the levels set properly, as Richie mentions.

2

Based on the other answers which use converting to character I'm using the following function to concatenate factors:

concat.factor <- function(...){
  as.factor(do.call(c, lapply(list(...), as.character)))
}

You can use this function just as you would use c.

snaut
  • 1,793
  • 15
  • 30
1

For this reason I prefer to work with factors inside data.frames:

df <- data.frame(facs = as.factor(
      c("i", "want", "to", "be", "a", "factor", "not", "an", "integer") ))

and subset it using subset() or dplyr::filter() etc. rather than row indexes. Because I don't have meaningful subset criteria in this case, I will just use head() and tail():

df1 <- head(df, 4)
df2 <- tail(df, 2)

Then you can manipulate them quite easily, e.g.:

dfc <- rbind(df1, df2)
dfc$facs
#[1] i       want    to      be      an      integer
#Levels: a an be factor i integer not to want
Melkor.cz
  • 1,411
  • 14
  • 12
0

Here's another way to add to a factor variable when the setup is slightly different:

facs <- factor(1:3, levels=1:9,
               labels=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
facs[4:6] <- levels(facs)[4:6]
facs
# [1] i      want   to     be     a      factor
# Levels: i want to be a factor not an integer
gung - Reinstate Monica
  • 10,603
  • 7
  • 53
  • 74