Cleaning up factor levels (collapsing multiple levels/labels)

后端 未结 10 2058
礼貌的吻别
礼貌的吻别 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:38

    I add this answer to demonstrate the accepted answer working on a specific factor in a dataframe, since this was not initially obvious to me (though it probably should have been).

    levels(df$var1)
    # "0" "1" "Z"
    summary(df$var1)
    #    0    1    Z 
    # 7012 2507    8 
    levels(df$var1) <- list("0"=c("Z", "0"), "1"=c("1"))
    levels(df$var1)
    # "0" "1"
    summary(df$var1)
    #    0    1 
    # 7020 2507
    

提交回复
热议问题