减少每个因子 dplyr 方法的级别数

Reduce number of levels for each factor dplyr approach

我正在尝试减少数据中每个因子变量的水平数。我想减少执行 2 个操作的级别数:

  1. 如果级别数大于截止值,则将频率较低的级别替换为新级别,直到级别数达到截止值
  2. 将观测值不足的因子中的水平替换为新水平

我写了一个运行良好的函数,但我不喜欢这段代码。水平 REMAIN 是否没有足够的观察并不重要。我更喜欢 dplyr 方法。

ReplaceFactor <- function(data, max_levels, min_values_factor){
    # First make sure that not to many levels are in a factor
    for(i in colnames(data)){
        if(class(data[[i]]) ==  "factor"){
            if(length(levels(data[[i]])) > max_levels){
                levels_keep <- names(sort(table(data[[i]]), decreasing = T))[1 : (max_levels - 1)]
                data[!get(i) %in% levels_keep, (i) := "REMAIN"]
                data[[i]] <- as.factor(as.character(data[[i]]))
            }
        } 
    }
    # Now make sure that in each level has enough observations
    for(i in colnames(data)){
        if(class(data[[i]]) ==  "factor"){
            if(min(table(data[[i]])) < min_values_factor){
                levels_replace <- table(data[[i]])[table(data[[i]]) < min_values_factor]
                data[get(i) %in% names(levels_replace), (i) := "REMAIN"]
                data[[i]] <- as.factor(as.character(data[[i]]))
            }
        } 
    }
    return(data)
}
df <- data.frame(A = c("A","A","B","B","C","C","C","C","C"), 
                 B = 1:9, 
                 C = c("A","A","B","B","C","C","C","D","D"), 
                 D = c("A","B","E", "E", "E","E","E", "E", "E"))
str(df)
'data.frame':   9 obs. of  4 variables:
 $ A: Factor w/ 3 levels "A","B","C": 1 1 2 2 3 3 3 3 3
 $ B: int  1 2 3 4 5 6 7 8 9
 $ C: Factor w/ 4 levels "A","B","C","D": 1 1 2 2 3 3 3 4 4
 $ D: Factor w/ 3 levels "A","B","E": 1 2 3 3 3 3 3 3 3

dt2 <- ReplaceFactor(data = data.table(df),
              max_levels = 3,
              min_values_factor = 2)
str(dt2)
Classes ‘data.table’ and 'data.frame':  9 obs. of  4 variables:
 $ A: Factor w/ 3 levels "A","B","C": 1 1 2 2 3 3 3 3 3
 $ B: int  1 2 3 4 5 6 7 8 9
 $ C: Factor w/ 3 levels "A","C","REMAIN": 1 1 3 3 2 2 2 3 3
 $ D: Factor w/ 2 levels "E","REMAIN": 2 2 1 1 1 1 1 1 1
 - attr(*, ".internal.selfref")=<externalptr>
 dt2
   A B      C      D
1: A 1      A REMAIN
2: A 2      A REMAIN
3: B 3 REMAIN      E
4: B 4 REMAIN      E
5: C 5      C      E
6: C 6      C      E
7: C 7      C      E
8: C 8 REMAIN      E
9: C 9 REMAIN      E

使用forcats

library(dplyr)
library(forcats)

max_levels <- 3
min_values_factor <- 2
df %>% 
  mutate_if(is.factor, fct_lump, n = max_levels, 
            other_level = "REMAIN", ties.method = "first") %>% 
  mutate_if(is.factor, fct_lump, prop = (min_values_factor - 1) / nrow(.), 
            other_level = "REMAIN")

#   A B      C      D
# 1 A 1      A REMAIN
# 2 A 2      A REMAIN
# 3 B 3      B      E
# 4 B 4      B      E
# 5 C 5      C      E
# 6 C 6      C      E
# 7 C 7      C      E
# 8 C 8 REMAIN      E
# 9 C 9 REMAIN      E

(哦,我无法复制您函数的确切行为,但您可以通过调整 ties.method 并将 1 减去 max_levels 来获得您想要的结果)。