I want to filter a dataframe for dates greater than or equal to something, but only for certain rows according to a condition, without affecting the rest of the observations.
I am not sure how to do this.
This is the code I have so far:
library(dplyr)
dat <- tibble(id = rep(LETTERS[1:4], 10),
date_a = c(
seq(as.Date("2020-01-01"), as.Date("2021-08-01"), "months"),
seq(as.Date("2020-01-01"), as.Date("2021-08-01"), "months")
))
dat |>
filter(date_a >= as.Date("2021-01-01"))
CodePudding user response:
We may use
tibble(
id = rep(LETTERS[1:4],10),
date_a = c(seq(as.Date("2020-01-01"), as.Date("2021-08-01"), "months"),
seq(as.Date("2020-01-01"), as.Date("2021-08-01"), "months")
)
) |>
filter((date_a >= as.Date("2021-01-01") & id == 'A')| (id != 'A'))