Question: Why does appending a character vector to a data frame (using either
cbind
ordata.frame
) turn the character vector into a factor vector?Does anyone know of functions besides
cbind
ordata.frame
which will allow me to avoid this unexpected and undesired behavior?
Here's a MWE:
ab = data.frame(a= c("a", "a"), b= c("b", "b"))
c = c("c", "c")
class(c)
'character'
abc1 = data.frame(ab, c)
class(abc1$c)
'factor'
class(abc1$a)
'character'
class(abc1$b)
'character'
abc2 = cbind(ab, c)
class(abc2$c)
'factor'
class(abc2$a)
'character'
class(abc2$b)
'character'
This behavior would make a little more sense to me if the columns of the original data frame had been factors, or were being converted to factors too during the process of appending the third column, but at least with my version of R that does not seem to be the case.