【问题标题】:Drop unused levels from a factor after filtering data frame using dplyr使用 dplyr 过滤数据帧后从因子中删除未使用的级别
【发布时间】:2018-01-31 15:22:27
【问题描述】:

我使用 dplyr 函数创建了一个新数据集,其中包含少于 4 行的名称。

df <- data.frame(name = c("a", "a", "a", "b", "b", "c", "c", "c", "c"), x = 1:9)

aa = df %>%
    group_by(name) %>%
    filter(n() < 4)

但是当我输入时

table(aa$name)

我明白了,

a b c 
3 2 0 

我希望我的输出如下

a b 
3 2 

如何将新帧 aa 与 df 完全分离?

【问题讨论】:

  • 您可以使用 droplevels 删除未使用的因子水平(如 c)
  • 谢谢。这正是我想要的。

标签: r dplyr plyr


【解决方案1】:

要完成您的答案和 KoenV 的评论,您可以将您的解决方案写在一行中或应用函数 factor 将删除未使用的级别:

table(droplevels(aa$name))
table(factor(aa$name))

或者因为您使用的是dplyr,所以在末尾添加droplevels

aa <- df %>%
       group_by(name) %>%
       filter(n() < 4) %>% 
       droplevels()
table(aa$name)

# Without using table
df %>%
  group_by(name) %>%
  summarise(count = n()) %>% 
  filter(count < 4)

【讨论】:

    【解决方案2】:
     aaNew <- droplevels(aa)
     table(aa$name)
    

    【讨论】:

    • 请在您的回答中添加一个小解释
    • 不鼓励仅使用代码的答案;你介意解释一下你在做什么,以便未来的读者更容易理解吗?
    猜你喜欢
    • 1970-01-01
    • 2021-07-28
    • 2015-01-05
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 2017-07-16
    相关资源
    最近更新 更多