我有一个周期列表,我想将其转换为像向量一样的行为(最终作为数据框中的列添加(。
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"))
在这种情况下,unlist
和purrr::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
看这里: 为什么 unlist(( 会杀死 R 中的日期