简体   繁体   English

如何将 timeDate 列表合并为单个 timeDate?

[英]How to combine a list of timeDate into a single timeDate?

I can generate a list of timeDate objects for New York Exchange.我可以为纽约交易所生成一个timeDate对象列表。 However, most of the analytical functions expect a single timeDate object.但是,大多数分析函数都需要单个timeDate对象。 The underlying data representation is POSIXct , so I can't just append them like a vector or a list.底层数据表示是POSIXct ,所以我不能像向量或列表一样附加它们。

How to do it?怎么做?

library(timeDate)
x <- lapply(c(1885: 1886), holidayNYSE)
x

[[1]]
NewYork
[1] [1885-01-01] [1885-02-23] [1885-04-03] [1885-11-03] [1885-11-26] [1885-12-25]

[[2]]
NewYork
[1] [1886-01-01] [1886-02-22] [1886-04-23] [1886-05-31] [1886-07-05] [1886-11-02] [1886-11-25]

class(x[[1]])
[1] "timeDate"
attr(,"package")
[1] "timeDate"

class(x[[1]]@Data)
[1] "POSIXct" "POSIXt"

# ??? How to my two datetime objects ???

We can use do.call with c我们可以将do.callc

x1 <- do.call(c, x)
x1
#NewYork
#[1] [1885-01-01] [1885-02-23] [1885-04-03] [1885-11-03] [1885-11-26] [1885-12-25] [1886-01-01] [1886-02-22] [1886-04-23] [1886-05-31] [1886-07-05] [1886-11-02]
#[13] [1886-11-25]

str(x1)
#Formal class 'timeDate' [package "timeDate"] with 3 slots
#  ..@ Data     : POSIXct[1:13], format: "1885-01-01 05:00:00" "1885-02-23 05:00:00" "1885-04-03 05:00:00" "1885-11-03 05:00:00" ...
#  ..@ format   : chr "%Y-%m-%d"
#  ..@ FinCenter: chr "NewYork"

and the structure of OP's list is OP list的结构是

str(x)
#List of 2
#$ :Formal class 'timeDate' [package "timeDate"] with 3 slots
#  .. ..@ Data     : POSIXct[1:6], format: "1885-01-01 05:00:00" "1885-02-23 05:00:00" "1885-04-03 05:00:00" "1885-11-03 05:00:00" ...
#  .. ..@ format   : chr "%Y-%m-%d"
#  .. ..@ FinCenter: chr "NewYork"
# $ :Formal class 'timeDate' [package "timeDate"] with 3 slots
#   .. ..@ Data     : POSIXct[1:7], format: "1886-01-01 05:00:00" "1886-02-22 05:00:00" "1886-04-23 05:00:00" "1886-05-31 05:00:00" ...
#  .. ..@ format   : chr "%Y-%m-%d"
#  .. ..@ FinCenter: chr "NewYork"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM