简体   繁体   English

在一个组中过滤 dataframe,其中一列满足 R 中的 AND 条件

[英]Filter dataframe within a group with one column meeting an AND condition in R

I have the following dataframe for which I need to filter only those rows that have both an "intake" and "discharge" per group (id).我有以下 dataframe ,我只需要过滤那些每组(id)同时具有“进气”和“排气”的行。 The result should go from looking like this:结果应该是 go 看起来像这样:

> df <- tibble(id = c(1, 1, 2, 3, 3, 3, 4, 4, 5, 6, 7, 7),
+              type = c("intake", "discharge", "intake", "intake", "discharge", "other",
+                       "intake", "discharge", "intake", "intake", "intake", "discharge"))
> df
      id type     
   <dbl> <chr>    
 1     1 intake   
 2     1 discharge
 3     2 intake   
 4     3 intake   
 5     3 discharge    
 6     4 intake   
 7     4 discharge
 8     5 intake   
 9     6 intake   
10     7 intake   
11     7 discharge

To this:对此:

      id type     
   <dbl> <chr>    
 1     1 intake   
 2     1 discharge   
 3     3 intake   
 4     3 discharge    
 5     4 intake   
 6     4 discharge 
 7     7 intake   
 8     7 discharge

So that groups (ids) that do not have both an intake AND a discharge are removed (and only those that do have both are kept).这样就删除了既不具有摄入量又不具有排出量的组(ID)(并且仅保留同时具有两者的组)。

I hope that makes sense... sorry it has been a long day.我希望这是有道理的......对不起,这是漫长的一天。

library(dplyr)
df %>%
  group_by(id) %>%
  filter(sum(type == "intake") >= 1,
         sum(type == "discharge") >= 1) %>%
  # add below if we only want intake/discharge lines
  # filter(type %in% c("intake", "discharge")) %>% 
  ungroup()

Result (varies due to addition of "other" in OP, unclear desired behavior)结果(由于在 OP 中添加了“其他”,期望的行为不清楚)

# A tibble: 9 x 2
     id type     
  <dbl> <chr>    
1     1 intake   
2     1 discharge
3     3 intake   
4     3 discharge
5     3 other    
6     4 intake   
7     4 discharge
8     7 intake   
9     7 discharge

Here's a way to select groups that have both "intake" and "discharge" .这是 select 组的一种方法,它同时具有"intake""discharge"

library(dplyr)

values <- c('intake', 'discharge')

df %>%
  group_by(id) %>%
  filter(all(values %in% type) & type %in% values) %>%
  ungroup

#     id type     
#  <dbl> <chr>    
#1     1 intake   
#2     1 discharge
#3     3 intake   
#4     3 discharge
#5     4 intake   
#6     4 discharge
#7     7 intake   
#8     7 discharge

all(values %in% type) selects the complete group which has both the values whereas type %in% values would select within those groups rows which has either of the two values. all(values %in% type)选择具有两个值的完整组,而type %in% values将 select 在具有两个值之一的组行中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R:如何将数据框中的行分组,满足条件的ID行,然后删除该组的先前行? - R: How can I group rows in a dataframe, ID rows meeting a condition, then delete prior rows for the group? R:根据一列中的条件过滤行 - R: filter rows based on a condition in one column 如何根据满足特定条件的所有行过滤具有匹配列值的多行? [R] - How do I filter multiple rows with matching column values based on all rows meeting a certain condition? [R] 根据 R 中数据帧中另一列的条件过滤数据帧 - filter dataframe based on condition on another column in the dataframe in R 我想过滤组 id 在 r 中的列和某些行值上满足的特定条件 - I want to filter group id's specific conditions meeting on both column and some row value in r 如果使用 Dplyr R 在另一列中满足条件,则按列过滤数据框 - Filter a dataframe by a column if a condition is met in another column with Dplyr R 使用 dplyr 在组会议条件中聚合行 - Aggregate rows within group meeting condition using dplyr R Dataframe:按行,按行聚合列内的字符串 - R Dataframe: aggregating strings within column, across rows, by group [修改]如何在满足R条件的列中查找最后一个值? - [modified]How to find the last value in a column meeting a condition in R? 根据满足 R 中 2 个 as.Date 列中的条件创建列 - Create a column based on meeting a condition in 2 as.Date columns in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM