0

I just stumbled upon a strange conversion aspect of the c() function's treatment of factors in R 3.3.1. Here is a basic example:

> x <- factor(c("X", "X", "Y", "Y", "X"))
> x[2] <- NA
> x
[1] X    <NA> Y    Y    X
Levels: X Y

all good. but how do I push an <NA> onto x now? the problem is that the c() function seems to convert factors into integers, so

> c(x, NA)
> c(x,NA)
[1]  1 NA  2  2  1 NA
> c(x)
[1] 1 1 2 2 1

this seems odd. logically, the c() function should not convert factors to integers when not necessary. (curious: is there a good reason?) is there a c() function for factors?

Hack-R
  • 22,422
  • 14
  • 75
  • 131
ivo Welch
  • 2,427
  • 2
  • 23
  • 31
  • 3
    The documentation has a lot to say about this (https://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html). "Note that factors are treated only via their internal integer codes " this works for factors `c.factor <- function(..., recursive=TRUE) unlist(list(...), recursive=recursive)` – Hack-R Sep 07 '16 at 23:01
  • 1
    `factor(c(levels(x)[x], NA))` and `factor(c(NA, levels(x)[x]))` for both directions of concatenation. – thelatemail Sep 07 '16 at 23:13
  • thanks. I had looked at my 3.3.1 docs, and this is new docs. (I should have upgraded sooner!) but, is there an underlying reason for this behavior by c()? – ivo Welch Sep 08 '16 at 00:05

0 Answers0