How to concatenate factors, without them being converted to integer level?

后端 未结 8 906
粉色の甜心
粉色の甜心 2020-11-28 10:12

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:

>         


        
8条回答
  •  日久生厌
    2020-11-28 10:54

    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))
    

提交回复
热议问题