How recognize weekday depending on entry date

I have a simple question, but I wanted to know how to do it: I have a database and two input data (dmda and DTest):

df1 <- structure(
  list(date = c("2021-06-23","2021-06-24","2021-06-30","2021-07-01"),
       DTT= c("Hol","Hol","Hol",0),
       Week= c("Wednesday day","Thursday day","Wednesday day","Thursday day"),
       Category = c("ABC","FDE","ABC","FDE"),
       DR01 = c(4,1,2,3), DR02= c(4,2,0,2),DR03= c(9,5,0,1),
       DR04 = c(5,4,3,2),DR05 = c(5,4,0,2)),
  class = "data.frame", row.names = c(NA, -4L))
   
  dmda<-"2021-07-01"
    DTest <-"0"

df2<-df1%>%
group_by(Category,Week, DTT) %>%
summarize(across(starts_with("DR"), median),.groups = 'drop')

> df2
# A tibble: 3 x 8
  Category Week      DTT    DR01  DR02  DR03  DR04  DR05
  <chr>    <chr>     <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 ABC      Wednesday Hol       3     2   4.5     4   2.5
2 FDE      Thursday  0         3     2   1       2   2  
3 FDE      Thursday  Hol       1     2   5       4   4 

If I want to make an if condition I can do it like this:

if(any(df2$DTT == DTest & df2$Week == "Thursday day" , na.rm = TRUE)) {
....
}

Now my doubt is that I didn't want to use df2$Week == "Thursday day" but rather use dmda that was defined. In other words, is there any way, from the date I chose in dmda, for the code to identify what day of the week it is?

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

dmda <- "2021-07-01"
wday(ymd(dmda))
#> [1] 5
wday(ymd(dmda), label = TRUE)
#> [1] Thu
#> Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
wday(ymd(dmda), label = TRUE, abbr = FALSE)
#> [1] Thursday
#> 7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday
as.character(wday(ymd(dmda), label = TRUE, abbr = FALSE))
#> [1] "Thursday"

I am not sure I understand the question but I would convert df1$date & dmda to actual dates and proceed from there.

library(lubridate)
df1$date  <-   ymd(df1$date)
dmda <-   ymd(dmda)

using lubridate

 wday(dmda)

using Base dates

weekdays(dmda)

Thanks for the answer @technocrat , but I have questions, if you can reply. From your code, what would df2$Week == look like in the if condition,?

suppressPackageStartupMessages({
  library(dplyr)
  library(lubridate)
})

dmda <- wday(ymd("2021-07-01"), label = TRUE, abbr = FALSE)

# DTest <-"0"

DTest <-  FALSE

# using FALSE for final element of DTT
# omitting "day" from day of week name
# uppercasing Date to avoid nameclash

df1 <- data.frame(Date = ymd("2021-06-23","2021-06-24","2021-06-30","2021-07-01"),
       DTT= c("Hol","Hol","Hol",FALSE),
       Category = c("ABC","FDE","ABC","FDE"),
       DR01 = c(4,1,2,3), DR02= c(4,2,0,2),DR03= c(9,5,0,1),
       DR04 = c(5,4,3,2),DR05 = c(5,4,0,2))

df1 <- df1 %>% mutate(Week = wday(Date, label = TRUE, abbr = FALSE)) %>%
  select(Date,DTT,Week,everything())

df2 <- df1 %>%
  group_by(Category,Week, DTT) %>%
  summarize(across(starts_with("DR"), median),.groups = 'drop')

df2
#> # A tibble: 3 × 8
#>   Category Week      DTT    DR01  DR02  DR03  DR04  DR05
#>   <chr>    <ord>     <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 ABC      Wednesday Hol       3     2   4.5     4   2.5
#> 2 FDE      Thursday  FALSE     3     2   1       2   2  
#> 3 FDE      Thursday  Hol       1     2   5       4   4

if(any(df2$DTT == DTest & df2$Week == "Thursday" , na.rm = TRUE)) print("Hit")
#> [1] "Hit"

ifelse(any(df2$DTT == DTest & df2$Week == dmda , na.rm = TRUE), "Hit","Miss")
#> [1] "Hit"


dmda <- wday(ymd("2021-07-02"), label = TRUE, abbr = FALSE)
ifelse(any(df2$DTT == DTest & df2$Week == dmda , na.rm = TRUE), "Hit","Miss")
#> [1] "Miss"

This topic was automatically closed 21 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.