How adjust order output table

Can tell me why the order is incorrect in my generated table below?. I filtered from 11/07 (Sunday) to 11/13 (Saturday). But as you can see, in the generated table, Saturday is getting by the second and not the last. The rest is OK, only Saturday went wrong.

    library(dplyr)
    Test <- structure(list(Week = c("Monday", "Tuesday", "Wednesday", "Thursday","Friday","Saturday","Sunday"),
                      Category = c("FDE", "FDE", "FDE", "FDE","FDE","FDE","FDE"),
                       time = c(4, 6, 3, 2,3,3,4)), class = "data.frame",row.names = c(NA, -7L))
 
      wk_port2eng <- data.frame(
        WeekE = c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"),
        WeekP = c("segunda-feira", "terca-feira", "quarta-feira", "quinta-feira",  "sexta-feira", "sabado", "domingo")
      )
    
    left_join(Test, wk_port2eng, by = c("Week" = "WeekE")) %>%      
      arrange(match(WeekP, weekdays(as.Date(c("2021-11-07", "2021-11-13"))))) %>%
     select(-WeekP)

             Week Category time
1    Sunday      FDE    4
2  Saturday      FDE    3
3    Monday      FDE    4
4   Tuesday      FDE    6
5 Wednesday      FDE    3
6  Thursday      FDE    2
7    Friday      FDE    3

There's a better approach:

suppressPackageStartupMessages({
  library(lubridate)
})
the_days <- seq(as.Date("2021-11-07"), by = "day", length.out = 7)

os_dias <- wday(the_days, label = TRUE, abbr = FALSE, locale = "pt_PT.utf8")
os_dias
#> [1] domingo segunda terça   quarta  quinta  sexta   sábado 
#> Levels: domingo < segunda < terça < quarta < quinta < sexta < sábado

meus_dias <- c("segunda-feira", "terca-feira", "quarta-feira", "quinta-feira",  "sexta-feira", "sabado", "domingo")

levels(os_dias) <- meus_dias
os_dias
#> [1] segunda-feira terca-feira   quarta-feira  quinta-feira  sexta-feira  
#> [6] sabado        domingo      
#> 7 Levels: segunda-feira < terca-feira < quarta-feira < ... < domingo

Thanks for reply @technocrat , when I run: os_dias <- wday(the_days, label = TRUE, abbr = FALSE, locale = "pt_PT.utf8"), appear:

Error in Sys.setlocale("LC_TIME", locale) : 
  (converted from warning) OS reports request to set locale to "pt_PT.utf8" cannot be honored

This may be OS specific; this works on macOS and Ubuntu. But thinking about it, just use wday without the locale argument and then just levels()

It worked @technocrat without locale argument but how do I merge this approach you took with my Test database to generate the output table?

Just subset

Test[,1] <-  os_dias

I’m not a console, so let me know.

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.