Reputation: 89
dose1 <- c("2022-08-21","2022-09-21","2022-10-21","2022-06-21","2022-09-21","2022-09-21")
dose2 <-c("2022-07-21","2022-09-21","2022-09-21","2022-10-21","2022-09-21","2022-09-21")
dose3 <- c("2022-02-21","2022-09-21","2022-09-21","2022-08-21","2022-09-21","2022-09-21")
count_dose1<- c("50","200","250","50","15","2")
count_dose2<- c("10","20","37","10","15","20")
count_dose3<- c("90","280","24","79","58","12")
df<- data.frame(dose1,dose2, dose3, count_dose1,count_dose2,count_dose3)
I wanted the data from date 15-09-2022 to 15-10- 2022. I was looking for a way to filter the data based on the date range from all three columns. I was able to do it one column after another by df%>% filter(dose1 > as.Date('2022-09-15') & dose1 <as.Date('2022-10-15'))
Please let me know if there is any way to create a data frame with the dates that falls in the range from 15-09-2022 to 15-10- 2022.
Upvotes: 0
Views: 459
Reputation: 52004
You can use filter
with if_all
and between
from dplyr
:
library(dplyr)
df %>%
filter(if_all(dose1:dose3, ~ between(.x, as.Date('2022-09-15'), as.Date('2022-10-15'))))
dose1 dose2 dose3 count_dose1 count_dose2 count_dose3
1 2022-09-21 2022-09-21 2022-09-21 200 20 280
2 2022-09-21 2022-09-21 2022-09-21 15 15 58
3 2022-09-21 2022-09-21 2022-09-21 2 20 12
Upvotes: 1