【问题标题】:Convert list of Lubridate periods to vector of periods [duplicate]将 Lubridate 周期列表转换为周期向量 [重复]
【发布时间】:2020-05-13 17:15:53
【问题描述】:

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

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】:
    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-23
      • 2017-08-07
      • 2018-01-03
      • 2014-05-05
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 2021-11-10
      相关资源
      最近更新 更多