Convert 20201223 to date format

Advertisements

How can I convert the format of column time in my data frame to date?

structure(list(geo = c("Alemanya", "Zona Euro", "Espanya", "Unió Europea", 
"França", "Itàlia"), time = c(20230201, 20230201, 20230201, 
20230201, 20230201, 20230201), values = c(0.5, 2, -0.9, 2.1, 
1.2, -2.3)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", 
"data.frame"))

I would like to change it to aaaa-mm-dd.

>Solution :

library(tidyverse)


df |> 
  mutate(date = ymd(time))
#> # A tibble: 6 × 4
#>   geo              time values date      
#>   <chr>           <dbl>  <dbl> <date>    
#> 1 Alemanya     20230201    0.5 2023-02-01
#> 2 Zona Euro    20230201    2   2023-02-01
#> 3 Espanya      20230201   -0.9 2023-02-01
#> 4 Unió Europea 20230201    2.1 2023-02-01
#> 5 França       20230201    1.2 2023-02-01
#> 6 Itàlia       20230201   -2.3 2023-02-01

Created on 2023-04-13 with reprex v2.0.2

Leave a ReplyCancel reply