简体   繁体   中英

Using dplyr to filter on multiple columns

I'm stumped on something that seems so silly. Is there an elegant dplyr way to filter out only the 2 rows where year == 2020 and quarter %in% 1:2 .

I don't want to filter quarter for any other year besides 2020.

library(tibble)
library(dplyr)

df <- tibble(measure = rep(letters[1:4], 4),
             year = rep(2017:2020,4)) %>% 
  arrange(year) %>% 
  mutate(quarter = rep(1:4, 4))

df2 <- filter(df, measure != 2020 & quarter %in% 1:2)

Created on 2021-02-01 by the reprex package (v0.3.0)

What i want is all but the last two rows:

在此处输入图像描述

Try negating the entire expression of what you don't want:

dplyr::filter(df, !(year == 2020 & quarter %in% 1:2))

   measure year quarter
1        a 2017       1
2        a 2017       2
3        a 2017       3
4        a 2017       4
5        b 2018       1
6        b 2018       2
7        b 2018       3
8        b 2018       4
9        c 2019       1
10       c 2019       2
11       c 2019       3
12       c 2019       4
13       d 2020       3
14       d 2020       4

year == 2020 & quarter %in% 1:2 says to keep rows where the year is 2020 AND quarter is 1 or 2 . The ! negates the entire expression so you exclude those rows.

FYI, you can also use dplyr::between(quarter, 1, 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM