将Lubridate周期列表转换为周期向量[重复]

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

我有一个周期列表,我想把它转换为一个向量(最终作为数据框架中的一列添加)。

library(lubridate)

x <- list(ms("09:10"),  ms("09:02"), ms("1:10"))

# some_function(x)
# with output
ms(c("09:10", "09:02", "1:10"))

unlistpurrr::flatten 在这种情况下是行不通的,因为它失去了周期属性。

r lubridate period
1个回答
1
投票
d <- do.call("c", x)
class(d)
[1] "Period"
attr(,"package")
[1] "lubridate"

 d <- data.frame(date = do.call("c", x))

 str(d)
'data.frame':   3 obs. of  1 variable:
 $ date:Formal class 'Period' [package "lubridate"] with 6 slots
  .. ..@ .Data : num  10 2 10
  .. ..@ year  : num  0 0 0
  .. ..@ month : num  0 0 0
  .. ..@ day   : num  0 0 0
  .. ..@ hour  : num  0 0 0
  .. ..@ minute: num  9 9 1

d
    date
1 9M 10S
2  9M 2S
3 1M 10S

请看这里。为什么在R中unlist()会杀死日期?

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