Home > Back-end >  How to make a range between dates and only some specific lines appear
How to make a range between dates and only some specific lines appear

Time:04-27

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%>           
  •  Tags:  
  • r
  • Related