将润滑周期列表转换为周期向量

Convert list of Lubridate periods to vector of periods

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

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 在这种情况下不起作用,因为它失去了周期属性。

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

看这里:why does unlist() kill dates in R