Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用对角矩阵创建时间序列_R_Dataframe_Time Series - Fatal编程技术网

R 使用对角矩阵创建时间序列

R 使用对角矩阵创建时间序列,r,dataframe,time-series,R,Dataframe,Time Series,假设我的数据具有以下结构: structure(list(Year = c(2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2001, 2001, 2001, 2001), Month = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

假设我的数据具有以下结构:

structure(list(Year = c(2000, 2000, 2000, 2000, 2000, 2000, 2000, 
2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
2000, 2000, 2001, 2001, 2001, 2001), Month = c(1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Day = c(1, 
1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 1, 1, 
1, 1), FivMin = c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 
4, 1, 2, 3, 4, 1, 2, 3, 4), A = c(1, 2, 3, 0, 1, 5, 3, 4, 1, 
0, 3, 1, 0, 2, 3, 0, 1, 2, 0, 9, 1, 2, 3, 0), B = c(2, 3, 4, 
1, 2, 3, 0, 1, 2, 1, 4, -2, 2, 1, 0, 2, 2, 3, -1, 1, 2, 3, 4, 
1), C = c(3, 0, 1, 2, 3, 4, 1, 9, 3, 7, 1, 2, 3, 4, 1, 2, 3, 
4, 1, 2, 3, 0, 1, 2), D = c(4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 
3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3)), row.names = c(NA, -24L
), class = c("tbl_df", "tbl", "data.frame"))
我的想法是每天使用
crossproduct
comand。为此,我编写了以下代码:

res <- lapply(split(data, data[c("Year","Month","Day")]), 
          function(x) tcrossprod(t(x[c("A","B","C","D")])))
Final<-do.call(rbind, lapply(res, diag))
我需要的是一个时间序列(矩阵或df对象),它由用
叉积计算的对角线组成,这意味着我想要的时间序列是

          A  B   C  D
2000.1.1 14 30  14 30
2000.1.2 51 14 107 30
2000.1.3 11 25  63 30
2000.1.4 13  9  30 30
2000.1.5 86 15  30 30
2001.1.1 14 30  14 30

我的原始代码会有什么变化。我想我可以用
grouped\u by
替换
split
命令,但它不起作用。

当拆分将数据帧放入列表时,它也会创建0行。只需删除那些零行,然后重试

ls<- split(data, data[c("Year","Month","Day")])
ls<- ls[sapply(ls, nrow)>0]
res <- lapply(ls, function(x) tcrossprod(t(x[c("A","B","C","D")])))
Final<-do.call(rbind, lapply(res, diag))
Final <- Final[ order(row.names(Final)), ]
Final

您可以使用
as.matrix
将看起来像矩阵的data.frame转换为矩阵。一旦你有了mtrix,你就可以做线性代数了。有
crossprod
%*%
diag
等。有关详细信息,请参阅example@asac,我需要每天计算
crossprod
,因为我需要创建
ts
。如果我仅将crossprod与一个矩阵一起使用,我将无法获得每个变量的时间序列。您可以在
tcrossprod
输出上应用
diag
ls<- split(data, data[c("Year","Month","Day")])
ls<- ls[sapply(ls, nrow)>0]
res <- lapply(ls, function(x) tcrossprod(t(x[c("A","B","C","D")])))
Final<-do.call(rbind, lapply(res, diag))
Final <- Final[ order(row.names(Final)), ]
Final
          A  B   C  D
2000.1.1 14 30  14 30
2000.1.2 51 14 107 30
2000.1.3 11 25  63 30
2000.1.4 13  9  30 30
2000.1.5 86 15  30 30
2001.1.1 14 30  14 30