-1

Question: Why does appending a character vector to a data frame (using either cbind or data.frame) turn the character vector into a factor vector?

Does anyone know of functions besides cbind or data.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.

Community
  • 1
  • 1
Chill2Macht
  • 1,182
  • 3
  • 12
  • 22

1 Answers1

2

As was pointed out in the comments, this has an easy fix.

abc = data.frame(ab, c, stringsAsFactors = FALSE)

This also was another question on StackOverflow, although I did not realize it at the time.

Change stringsAsFactors settings for data.frame

Chill2Macht
  • 1,182
  • 3
  • 12
  • 22
  • 1
    Should note that cbind also has a stringsAsFactors argument. Either one will work in this case, and you can choose either one depending on needs. – Devin May 28 '18 at 18:15