Cleaning up factor levels (collapsing multiple levels/labels)

后端 未结 10 2115
礼貌的吻别
礼貌的吻别 2020-11-22 14:27

What is the most effective (ie efficient / appropriate) way to clean up a factor containing multiple levels that need to be collapsed? That is, how to combine two or more fa

10条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 14:57

    Another way is to make a table containing the mapping:

    # stacking the list from Aaron's answer
    fmap = stack(list(Yes = c("Y", "Yes"), No = c("N", "No")))
    
    fmap$ind[ match(x, fmap$values) ]
    # [1] Yes  Yes  Yes  No   No   
    # Levels: No Yes
    
    # or...
    
    library(data.table)
    setDT(fmap)[x, on=.(values), ind ]
    # [1] Yes  Yes  Yes  No   No   
    # Levels: No Yes
    

    I prefer this way, since it leaves behind an easily inspected object summarizing the map; and the data.table code looks just like any other join in that syntax.


    Of course, if you don't want an object like fmap summarizing the change, it can be a "one-liner":

    library(data.table)
    setDT(stack(list(Yes = c("Y", "Yes"), No = c("N", "No"))))[x, on=.(values), ind ]
    # [1] Yes  Yes  Yes  No   No   
    # Levels: No Yes
    

提交回复
热议问题