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

查看:81
本文介绍了使用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天全站免登陆