使用 dplyr 分组和过滤数据管理 [英] group by and filter data management using dplyr

查看:26
本文介绍了使用 dplyr 分组和过滤数据管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

取一个简单的数据集

a <- c(1,2,3,4,5,6,7,8)
b <- c(1,2,2,1,2,2,2,2)
c <- c(1,1,1,2,2,2,3,3)
d <- data.frame(a,b,c)

现在我想过滤我的数据,以便我们group_by(c)然后删除所有没有b=1出现的数据.

now I want to filter my data, so that we group_by(c) and then remove all data where no b=1occurs.

因此结果 (e) 应该看起来像 d 但没有底部的两行

Thus the results (e) should look like d but without the two bottom rows

我试过使用

e <- d %>%
  group_by(c) %>%
  filter(n(b)>1)

输出应该包含下面绿色的数据并删除红色的数据

The output should contain the data in green below and remove the data in red

推荐答案

尝试

d %>% 
  group_by(c) %>% 
  filter(any(b == 1))

给出:

#Source: local data frame [6 x 3]
#Groups: c
#
#  a b c
#1 1 1 1
#2 2 2 1
#3 3 2 1
#4 4 1 2
#5 5 2 2
#6 6 2 2

这篇关于使用 dplyr 分组和过滤数据管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆