Home > Software engineering >  R:Problem with `mutate()` input `index_date`. x character string is not in a standard unambiguous fo
R:Problem with `mutate()` input `index_date`. x character string is not in a standard unambiguous fo

Time:03-20

in my dataset

mydat=structure(list(date = c("22.06.2021", "22.06.2021", "22.06.2021", 
"22.06.2021", "22.06.2021", "22.06.2021", "22.06.2021", "22.06.2021", 
"22.06.2021", "22.06.2021", "22.06.2021", "22.06.2021", "22.06.2021", 
"22.06.2021", "22.06.2021", "22.06.2021", "22.06.2021", "22.06.2021", 
"23.06.2021", "23.06.2021", "23.06.2021", "23.06.2021", "23.06.2021", 
"23.06.2021", "23.06.2021", "23.06.2021", "23.06.2021", "23.06.2021", 
"23.06.2021", "23.06.2021", "23.06.2021", "23.06.2021", "23.06.2021", 
"23.06.2021", "23.06.2021", "23.06.2021", "23.06.2021", "23.06.2021", 
"23.06.2021", "23.06.2021", "23.06.2021", "23.06.2021", "24.06.2021", 
"24.06.2021"), hour = c(6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L), weekday = c(2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L), base_price = c(3250.87, 
3261.89, 3272.91, 3283.93, 3294.95, 3305.97, 3316.98, 3328, 3339.02, 
3350.04, 3361.06, 3372.08, 3383.1, 3394.12, 3405.14, 3416.16, 
3427.17, 3438.19, 3449.21, 3460.23, 3471.25, 3482.27, 3493.29, 
3504.31, 3515.33, 3526.35, 3537.36, 3548.38, 3559.4, 3570.42, 
3581.44, 3592.46, 3603.48, 3614.5, 3625.52, 3636.54, 3647.55, 
3658.57, 3669.59, 3680.61, 3691.63, 3702.65, 3713.67, 3724.69
)), class = "data.frame", row.names = c(NA, -44L))

i am trying to convert date format

extended_data_mod <- mydat %>%
  dplyr::mutate(., 
                index_date = as.Date(paste0(lubridate::year(date), "-", lubridate::month(date), "-01")),
                months = lubridate::month(index_date),
                years = lubridate::year(index_date))

and always get error

Problem with `mutate()` input `index_date`.
x character string is not in a standard unambiguous format
i Input `index_date` is `as.Date(...)

if i use as.numeric(mydat$date) i get just NA. How can i get this format?(example) yyyy-mm-dd (2022-03-19)(better that my dataset was a tibble) Thank you for your help.

CodePudding user response:

To use functions like lubridate::month and lubridate::year you need data in date class. The input data has character class. lubridate::dmy would help you change to Date class and then you can extract month and year from it.

library(dplyr)
library(lubridate)

mydat %>%
  mutate(index_date = dmy(date),
         months = month(index_date),
         years = year(index_date))

#         date hour weekday base_price index_date months years
#1  22.06.2021    6       2    3250.87 2021-06-22      6  2021
#2  22.06.2021    7       2    3261.89 2021-06-22      6  2021
#3  22.06.2021    8       2    3272.91 2021-06-22      6  2021
#4  22.06.2021    9       2    3283.93 2021-06-22      6  2021
#5  22.06.2021   10       2    3294.95 2021-06-22      6  2021
#6  22.06.2021   11       2    3305.97 2021-06-22      6  2021
#...
#...
  • Related