从csv和fst格式读回时,我在恢复lubridate::interval时遇到问题。
有人对此有什么建议吗?
library(tidyverse)
library(fst)
library(lubridate)
test <- tibble(
start = ymd_hms("2020-01-01 12:13:14", tz="UTC"),
end = ymd_hms("2021-01-01 12:13:14", tz="UTC"),
interval = lubridate::interval(start, end)
) %>%
write_csv("test1.csv")
test %>% fst::write_fst("test1.fst")
str(test)
test_read_back_csv <- read_csv("test1.csv")
str(test_read_back_csv)
test_read_back_fst <- read_fst("test1.fst")
str(test_read_back_fst)
您将看到返回对象test_read_back_csv$interval或test_read_back_fst$interval的结构不是lubridate interval,我都需要保存此文件,并正确地将其读回。
保存为二进制格式,如.RDS
:
str(test)
#> tibble [1 x 3] (S3: tbl_df/tbl/data.frame)
#> $ start : POSIXct[1:1], format: "2020-01-01 12:13:14"
#> $ end : POSIXct[1:1], format: "2021-01-01 12:13:14"
#> $ interval:Formal class 'Interval' [package "lubridate"] with 3 slots
#> .. ..@ .Data: num 31622400
#> .. ..@ start: POSIXct[1:1], format: "2020-01-01 12:13:14"
#> .. ..@ tzone: chr "UTC"
write_rds(test, "test1.rds")
test_read_back_rds <- read_rds("test1.rds")
str(test_read_back_rds)
#> tibble [1 x 3] (S3: tbl_df/tbl/data.frame)
#> $ start : POSIXct[1:1], format: "2020-01-01 12:13:14"
#> $ end : POSIXct[1:1], format: "2021-01-01 12:13:14"
#> $ interval:Formal class 'Interval' [package "lubridate"] with 3 slots
#> .. ..@ .Data: num 31622400
#> .. ..@ start: POSIXct[1:1], format: "2020-01-01 12:13:14"
#> .. ..@ tzone: chr "UTC"
创建于2022-03-14由reprex包(v2.0.1(