我完全希望被一个重复的问题抨击,但是我找不到类似的问题。提前致歉。
我试图清除某些数据,这些数据有时包含摘要行,有时却不包含。这是一个可重现的小示例:
library(tidyverse)
yr <- c(2010, 2010, 2010,
2011, 2011, 2011, 2011,
2012, 2012, 2012)
a <- c("HAY", "APPLES", "PUMPKINS",
"HAY", "HAY & HAYLAGE", "APPLES", "PUMPKINS",
"HAY & HAYLAGE", "APPLES", "PUMPKINS")
b <- c(1:10)
dat <- as_tibble(list(yr = yr, a = a, b = b))
dat %>%
group_by(yr) %>%
filter(a != "HAY" if group contains a== "HAY & HAYLAGE")
很明显,最后一行代码是伪代码。在yr = 2011的组中,我想过滤掉等于“ HAY”的行。我得到的小标题应该有9行。
答案 0 :(得分:3)
这是一种实现方法-您可以在过滤条件中使用if
语句:
library(dplyr)
# (data from OP)
dat <- dplyr::tibble(
yr = c(2010, 2010, 2010, 2011, 2011,
2011, 2011, 2012, 2012, 2012),
a = c("HAY", "APPLES", "PUMPKINS", "HAY", "HAY & HAYLAGE",
"APPLES", "PUMPKINS", "HAY & HAYLAGE", "APPLES", "PUMPKINS"),
b = 1:10
)
dat %>%
group_by(yr) %>%
filter(if ('HAY & HAYLAGE' %in% a) a!='HAY' else TRUE) %>%
ungroup()
## result will be:
##
## # A tibble: 9 x 3
## yr a b
## <dbl> <chr> <int>
## 1 2010 HAY 1
## 2 2010 APPLES 2
## 3 2010 PUMPKINS 3
## 4 2011 HAY & HAYLAGE 5
## 5 2011 APPLES 6
## 6 2011 PUMPKINS 7
## 7 2012 HAY & HAYLAGE 8
## 8 2012 APPLES 9
## 9 2012 PUMPKINS 10