以 R / 日期格式上传 Tibble

问题描述 投票:0回答:1

有人可以帮我解决以下问题吗?我有一个 Excel 工作表,不同列中具有不同的日期格式。我想将此 excel 文件作为 tibble 上传到 R Studion 中,与 excel 文件中显示的方式完全相同。但是,如您所见,列“date2”、“date5”、“date6”、“date7”和“date8”的上传方式与其他列不同。如何修改我正在使用的代码 (df <- read_excel('raw_data.xlsx')? The object can stay a Character in the outcome as I can then convert it into a date format.

enter image description here

df<- read_excel('raw_data.xlsx')

enter image description here

谢谢您的帮助!!

我已经尝试将 check.names 设置为 FALSE,但这也不起作用

date tibble
1个回答
0
投票

Excel 可以将日期存储为“自起始日期以来的天数”。您可以按照下面的示例转换它们:

library(tidyverse)

df <- tribble(
  ~date1, ~date2,
  "2014-05-15", 41774,
  "2014-05-16", 41775
)

df |>
  mutate(date3 = as_date(date2, origin = "1899-12-30"))
#> # A tibble: 2 × 3
#>   date1      date2 date3     
#>   <chr>      <dbl> <date>    
#> 1 2014-05-15 41774 2014-05-15
#> 2 2014-05-16 41775 2014-05-16

创建于 2024-03-20,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.