Reputation: 153
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!
Upvotes: 1
Views: 33
Reputation: 388817
A regex approach using sub
-
sub('(\\d+)-(\\d+)-(\\d+)', '\\1\\2\\301', x$week)
# [1] "2020080901" "2017111101" "2017061801" "2020090701" "2020090701" "2020090701"
# [7] "2020091201" "2020082301" "2019122201" "2017102901"
Upvotes: 0
Reputation: 78917
Here is an alternative way:
library(dplyr)
library(stringr)
x %>%
mutate(week = paste0(str_remove_all(week, "-"), "01"))
week store
1 2020080901 14071
2 2017111101 11468
3 2017061801 2428
4 2020090701 17777
5 2020090701 14821
6 2020090701 10935
7 2020091201 5127
8 2020082301 14772
9 2019122201 14772
10 2017102901 14772
Upvotes: 1
Reputation: 886948
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%d01"))
-output
x
week store
1 2020080901 14071
2 2017111101 11468
3 2017061801 2428
4 2020090701 17777
5 2020090701 14821
6 2020090701 10935
7 2020091201 5127
8 2020082301 14772
9 2019122201 14772
10 2017102901 14772
Or using base R
x$week <- format(as.Date(x$week), "%Y%m%d01")
Upvotes: 2