Dropping factors which levels have observations smaller than a specific value-R

后端 未结 4 710
臣服心动
臣服心动 2021-01-16 19:49

Let I have such data frame(df1) with factors:

factor1  factor2  factor3
-------  -------  -------
d        a         x
d        a         x
b        a                


        
4条回答
  •  春和景丽
    2021-01-16 20:44

    I would create a quick helper function that checks how many unique instances of each level exist with a quick call to table() -- look at table(df$fac1) to see how this works. Note this isn't very robust, but should get you started:

    df <- data.frame(fac1 = factor(c("d", "d", "b", "b", "b", "c", "c", "c", "c")),
                     fac2 = factor(c("a", "a", "a", "c", "c", "c", "n", "n", "n")),
                     fac3 = factor(c(rep("x", 4), rep("y", 5))),
                     other = 1:9)
    
    at_least_three_instances <- function(column) {
      if (is.factor(column)) {
        if (min(table(column)) > 2) {
          return(TRUE)
        } else {
          return(FALSE)
        }
      } else {
        return(TRUE)
      }
    }
    
    df[unlist(lapply(df, at_least_three_instances))]
    

提交回复
热议问题