Home > Mobile >  trying to change the format of dates R
trying to change the format of dates R

Time:10-07

I have a df as follows:

x = data.frame(
  week = c("2020-08-09", "2017-11-11", "2017-06-18", "2020-09-07", "2020-09-07", "2020-09-07",
           "2020-09-12", "2020-08-23", "2019-12-22", "2017-10-29"),
  store = c(14071, 11468, 2428, 17777, 14821, 10935,  5127, 14772, 14772, 14772)
)

I am trying to remove the "-" in the week values and add "01" to the end of them so that "2020-08-09" would be "2020080901" and so on. Is there an easy way to do this using regex/some other method for the week column? Thanks!

CodePudding user response:

We may convert to Date, and remove the - with str_remove_all or use format and add the 01 at the end

library(lubridate)
library(dplyr)
x <- x %>%
     mutate(week = format(ymd(week), "%Y%m           
  • Related