How to make a range between dates and only some specific lines appear

The code below works fine. Notice that I have a function called f3 and then I do Output to generate all the data obtained from f3. Everything is OK! But now my idea is to make a range between dates and only some specific lines of Output appear.

Example:

I want to see the Time value obtained in the range from "2022-04-24" to "2022-04-25", that is, Sunday and Monday, so only the first and second line will appear. If I want to see between "2022-04-24" and "2022-04-26", which includes Sunday, Monday and Tuesday, the 3 output lines will appear. If I want to see only one day, that is, the range "2022-04-24" to "2022-04-24", only the second line will appear.

Is there any way to do this? I thought something like this:

  Output %>%
  arrange(match(Week, weekdays(seq(as.Date("2022-04-24"),as.Date("2022-04-25"), by = "day"))))

However, it didn't!

Code executable:

library(dplyr)
library(tidyr)
library(lubridate)

df1<- structure(
list(
Id = c(1, 1, 1, 1),
date1 = c("2022-01-06","2022-01-06","2022-01-06","2022-01-06"),
date2 = c("2022-01-02","2022-01-03","2022-01-04","2022-01-09"),
Week = c("Sunday","Monday","Tuesday","Sunday"),
DT=c(1,1,1,1),
Category = c("EFG", "ABC","EFG","EFG"),
Time = c(1,2,2,1)),row.names = c(NA, 4L), class = "data.frame")

      Id      date1      date2    Week DT Category Time
1  1 2022-01-06 2022-01-02  Sunday  1      EFG    1
2  1 2022-01-06 2022-01-03  Monday  1      ABC    2
3  1 2022-01-06 2022-01-03 Tuesday  1      EFG    2
4  1 2022-01-06 2022-01-09  Sunday  1      EFG    1


f3 <- function(df1) {
  
  nms <- c('Time|time')
  
  mtime <- df1 %>% 
    group_by(Id,Week = tools::toTitleCase(Week), Category,DT) %>% 
    summarise(across(matches(nms), mean, .names = 'Time',na.rm = TRUE), .groups = 'drop') %>% 
    mutate(Time = format(round(Time, digits = 2), nsmall = 2))
  
  return(mtime)
  
}

Generate for all datas

Output <- df1%>%f3()

    > Output
# A tibble: 3 x 5
     Id Week    Category    DT Time 
  <dbl> <chr>   <chr>    <dbl> <chr>
1     1 Monday  ABC          1 2.00 
2     1 Sunday  EFG          1 1.00 
3     1 Tuesday EFG          1 2.00 

Generate for a specific data

Output %>% 
  filter(Id=="1",Week == "Monday", Category == "ABC",DT=="1")

     Id Week   Category    DT Time 
  <dbl> <chr>  <chr>    <dbl> <chr>
1     1 Monday ABC          1 2.00 

Generate for a data range (ERROR)

Output %>%
  arrange(match(Week, weekdays(seq(as.Date("2022-04-24"),as.Date("2022-04-25"), by = "day"))))

     Id Week    Category    DT Time 
  <dbl> <chr>   <chr>    <dbl> <chr>
1     1 Monday  ABC          1 2.00 
2     1 Sunday  EFG          1 1.00 
3     1 Tuesday EFG          1 2.00 

For this example above, only Sunday and Monday would have to appear.

Is this what you want?

Output <- f3(df1)
Output %>% filter(Week %in% weekdays(seq(as.Date("2022-04-24"),as.Date("2022-04-25"), by = "day")))
# A tibble: 2 × 5
     Id Week   Category    DT Time 
  <dbl> <chr>  <chr>    <dbl> <chr>
1     1 Monday ABC          1 2.00 
2     1 Sunday EFG          1 1.00 
1 Like

Exactly! Thank you very much @FJCC !

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.