2

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:

enter image description here

2
  • df2 <- filter(df, measure != 2020 & quarter %in% 1:2) change 'measure' to 'year' ? Commented Feb 1, 2021 at 17:50
  • Does this answer your question? dplyr filter with condition on multiple columns Commented Feb 1, 2021 at 18:09

1 Answer 1

4

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, exactly what i needed!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.