【问题标题】:Is there a way in R to combine the functions slice_max (dplyr) and fct_other(forcats)?在 R 中有没有办法结合函数 slice_max (dplyr) 和 fct_other(forcats)?
【发布时间】:2020-09-19 20:32:28
【问题描述】:

我正在尝试将 dplyr 中的 slice_max 函数和 forcats 中的 fct_other 函数结合起来,以基于数字变量获取数据帧的前 n 个切片,但我不想丢失非前 n 个因子。如果需要,我希望将这些其他因素指定为“其他”,以便在此之后进行总结或计数。

例如,使用类似这样的数据框:

df <- data.frame(acron = c("AA", "BB", "CC", "DD", "EE", "FF", "GG"), value = c(6, 4, 1, 10, 3, 1, 1))

如果我想要前 3 个主题的“价值”,我可以使用下一个代码:

df %>% 
  slice_max(value, n = 3)

得到下一个结果:

acron 值
DD 10
AA 6
BB 4

但我想指定删除“acron”的因素“其他”类似于使用来自 forcats 的函数 fct_other 获得的结果。我试过这段代码,但它不起作用:

df %>% 
  mutate(acron = fct_other(acron, keep = slice_max(value, n = 3), other_level = "Others"))

有什么建议可以得到这样的东西吗?:

acron 值
DD 10
AA 6
BB 4
其他 3
其他 1
其他 1
其他 1

甚至像这样:

acron 值
DD 10
AA 6
BB 4
其他 6

【问题讨论】:

    标签: r dplyr tidyverse forcats


    【解决方案1】:

    一个选项可能是使用fct_lump_n()

    df %>%
     mutate(acron = fct_lump_n(acron, n = 3, w = value))
    
      acron value
    1    AA     6
    2    BB     4
    3 Other     1
    4    DD    10
    5 Other     3
    6 Other     1
    7 Other     1
    

    【讨论】:

    • 不错的答案。我之前没见过fct_lump,所以看看docs。似乎fct_lump 现在被认为是“历史性的”,并且首选其更具体的变体。例如。 fct_lump_min(acron, 4, value).
    • @andrew_reece 我没有注意到它已更新。感谢您指出这一点,相应地修改了帖子:)
    • @andrew_reece 我使用了fct_lump_n(),它没有min 参数,但你是对的,它也可以通过指定fct_lump_min() 参数来解决:)跨度>
    • @tmfmnk,谢谢。就是这样。关键是使用“w”作为基于另一个数值变量的加权参数。
    • @andrew_reece 建议它也可以工作,但是使用 fct_lump_min 您需要知道低于您想要将其他因素声明为“其他”的数值,在这种情况下为 4。它会派上用场未来,当然。
    【解决方案2】:

    如果我们想使用slice_max 的方法,它需要提取向量'acron'。使用pull,可以提取出来

    library(dplyr)
    library(forcats)
    df %>% 
       mutate(acron = fct_other(acron, keep =  {.} %>% 
                                        slice_max(value, n = 3) %>% 
                                        pull(acron), other_level = "Others"))
    #   acron value
    #1     AA     6
    #2     BB     4
    #3 Others     1
    #4     DD    10
    #5 Others     3
    #6 Others     1
    #7 Others     1
    

    或者其他选项是orderhead df %>% 变异(acron = fct_other(acron,keep = head(acron[order(-value)],3), other_level = "其他")) %>% 排列(描述(值)) # 首字母值 #1 DD 10 #2 AA 6 #3 BB 4 #4 其他 3 #5 其他 1 #6 其他 1 #7 其他 1


    或先执行arrange,然后使用

    df %>%
       arrange(desc(value)) %>%
       mutate(acron = fct_other(acron, keep = head(acron, 3), other_level = "Others"))
    #   acron value
    #1     DD    10
    #2     AA     6
    #3     BB     4
    #4 Others     3
    #5 Others     1
    #6 Others     1
    #7 Others     1
    

    要获得汇总输出,请按sum 分组

    df %>%
       arrange(desc(value)) %>%
       group_by(acron = fct_other(acron, keep = head(acron, 3), 
           other_level = "Others")) %>%
       summarise(value = sum(value))
    # A tibble: 4 x 2
    #  acron  value
    #  <fct>  <dbl>
    #1 AA         6
    #2 BB         4
    #3 DD        10
    #4 Others     6
    

    【讨论】:

    • 谢谢@akrun。您的方法是正确的,但是使用建议的函数 fct_lump_n 使用参数“w”作为权重会更简单
    猜你喜欢
    • 2019-12-19
    • 1970-01-01
    • 2011-02-23
    • 2022-01-20
    • 2021-12-18
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多