R: factor levels, recode rest to 'other'

后端 未结 4 1686
夕颜
夕颜 2021-02-08 03:28

I use factors somewhat infrequently and generally find them comprehensible, but I often am fuzzy about the details for specific operations. Currently, I am coding/collapsing cat

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-08 04:25

    I think the easiest way is to relabel all the naics not in the top 8 to a special value.

    data$naics[!(data$naics %in% top8)] = -99
    

    Then you can use the "exclude" option when turning it into a factor

    factor(data$naics, exclude=-99)
    

提交回复
热议问题