将20201223转换为日期格式。

huangapple go评论100阅读模式
英文:

Convert 20201223 to date format

问题

我该如何将数据框中的time列格式转换为日期格式(aaaa-mm-dd)?

英文:

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

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

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

答案1

得分: 2

以下是您提供的代码的翻译部分:

  1. library(tidyverse)
  2. df |>
  3. mutate(date = ymd(time))

请注意,我已将HTML实体字符(如>)保留为原样,因为它们可能是代码中的一部分。如果需要进一步的翻译或更多帮助,请告诉我。

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

<sup>Created on 2023-04-13 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年4月13日 18:38:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定