简体   繁体   English

Append 时间序列到 r 中的时间序列列表

[英]Append a time series to a list of time series in r

I have list of products: P1, P2, P3, P4 belonging to different categories, as per df CAT .我有产品清单:根据df CAT属于不同类别的P1、P2、P3、P4 For each product there are two time series associated: ts_sales and ts_ofs (representing sales and out of stock).对于每个产品,都有两个相关的时间序列ts_salests_ofs (代表销售和缺货)。

I want to correlate sales time series of P1 with out of stock of P2, P3 (belonging to the same category)我想将P1的销售时间序列与P2、P3的缺货相关联(属于同一类别)

This code shows how I correlate sales time series from products on the same category:此代码显示了我如何关联同一类别产品的销售时间序列

rm(list=ls())
CAT <- data.frame(PROD = c('P1','P2','P3','P4'),CAT = c('C1','C1','C1','C2'))
ts_sales <- list()
ts_sales$'P1' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)
ts_sales$'P2' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)
ts_sales$'P3' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)
ts_sales$'P4' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)
ts_ofs <- list()
ts_ofs$'P1' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)
ts_ofs$'P2' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)
ts_ofs$'P3' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)
ts_ofs$'P4' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)

sales <- with(CAT,split(as.character(PROD), CAT))
sales <-  lapply(sales, function(x) ts_sales[x])
ofs <- with(CAT,split(as.character(PROD), CAT))
ofs <-  lapply(ofs, function(x) ts_ofs[x])
cor(do.call(ts.intersect, sales$C1))

I tried:我试过了:

c(ofs$C1,sales$C1$P1)

but sales$C1$P1 does not get appended on the same formatsales$C1$P1不会以相同的格式附加

Appreciate:)欣赏:)

We can use我们可以用

out <- lapply(Map(c, ofs, sales), function(x) do.call(ts.intersect, x))

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

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