Adjust so that the week names change into an R code

I would like to know if there is a way to rename the weekday name information in a database. I have inserted the database I want to change below.

 df1 <- structure(list(date = c("2021-06-30", "2021-07-01", "2021-07-02", "2021-07-03","2021-07-04","2021-07-05","2021-07-06"),
 Week = c("Quarta-feira", "Quinta-feira", "Sexta-feira", "Sábado","Domingo","Segunda-feira","Terça-feira")), 
class = "data.frame", row.names = c(NA, -7L))

I would like to make the following change instead of Quarta-feira I would just like Quarta,Quinta-feira to Quinta, Sexta-feira to Sexta, Segunda-feira to Segunda, Terça-feira to Terça. Sábado and Domingo remain the same.

Note: these days of the week are written in Portuguese.

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(stringr)
# use Date, not date, which is the name of a built-in function
d <- data.frame(Date =  c("2021-06-30", "2021-07-01", "2021-07-02", "2021-07-03","2021-07-04","2021-07-05","2021-07-06"),
Week = c("Quarta-feira", "Quinta-feira", "Sexta-feira", "Sábado","Domingo","Segunda-feira","Terça-feira"))

# convert Date from character
d$Date <- ymd(d$Date)

d$dayname <- str_to_title(wday(d$Date, label = TRUE, abbr = FALSE, locale = "pt_PT.utf8"))

d
#>         Date          Week dayname
#> 1 2021-06-30  Quarta-feira  Quarta
#> 2 2021-07-01  Quinta-feira  Quinta
#> 3 2021-07-02   Sexta-feira   Sexta
#> 4 2021-07-03        Sábado  Sábado
#> 5 2021-07-04       Domingo Domingo
#> 6 2021-07-05 Segunda-feira Segunda
#> 7 2021-07-06   Terça-feira   Terça

Simply

df1$Week <- sub("-feira", "", df1$Week)

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.