保存和读取润滑间隔到磁盘/从磁盘

发布于 2025-01-14 11:22:17 字数 667 浏览 6 评论 0原文

从 csv 和 fst 格式读回时,我在恢复 lubridate::intervals 时遇到问题。

有人对如何做到这一点有建议吗?

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 间隔,我都需要保存此文件,并正确读回它。

I am having problems recovering lubridate::intervals when reading back from csv, and fst formats.

Does anyone have a suggestion for how to do this?

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)

You will see that the structure of the returned object test_read_back_csv$interval or test_read_back_fst$interval is not a lubridate interval, and I both need to save this file, and read it back properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风蛊 2025-01-21 11:22:17

保存为二进制格式,例如 .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"

reprex 包 (v2.0.1)

Save to a binary format such as .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"

Created on 2022-03-14 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文