Rename factor levels based on a condition in R

前端 未结 2 1257
我寻月下人不归
我寻月下人不归 2021-01-22 03:22

I want to combine all factors with a count less than n into one factor named \"Else\"

For example if n = 3 then in the following df I want to combine \"c\", \"d\" and \"

2条回答
  •  不思量自难忘°
    2021-01-22 04:01

    Why not something like this?

    library(data.table)
    dt <- data.table(df)
    dt[,ynew := ifelse(.N < 3, "else",as.character(y)), by = "y"]
    

提交回复
热议问题