Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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_R Table - Fatal编程技术网

R 以';扫掠';

R 以';扫掠';,r,r-table,R,R Table,我有一个单列矩阵(rbind操作的结果),初始起始值为几个时间序列的“starts”,从不同月份开始,如下所示: starts <- matrix(rep(100,5),ncol=1) rownames(starts) <- month.name[1:5] starts [,1] January 100 February 100 March 100 April 100 May 100 我想我可以通过扫描的方式

我有一个单列矩阵(rbind操作的结果),初始起始值为几个时间序列的“starts”,从不同月份开始,如下所示:

starts <- matrix(rep(100,5),ncol=1)
rownames(starts) <- month.name[1:5]
starts          
          [,1]
January   100
February  100
March     100
April     100
May       100
我想我可以通过扫描的方式使用一个函数来实现这一点,例如:

result <- sweep(starts,2,mytable,"-")

result不确定这是否理想,但这里有一个尝试:

starts[,rep(1,5)] - cbind(0, t(apply(mytable, 1, cumsum)) )

#         [,1] [,2] [,3] [,4] [,5]
#January   100   80   70   65   60
#February  100  100   90   80   70
#March     100  100  100   95   90
#April     100  100  100  100   90
#May       100  100  100  100  100

提醒一下:你可以从结果中剪下下面的三角形:
result[lower.tri(result)]
result <- sweep(starts,2,mytable,"-")
starts[,rep(1,5)] - cbind(0, t(apply(mytable, 1, cumsum)) )

#         [,1] [,2] [,3] [,4] [,5]
#January   100   80   70   65   60
#February  100  100   90   80   70
#March     100  100  100   95   90
#April     100  100  100  100   90
#May       100  100  100  100  100