我有8行41列的数据。列标题为8/1 / 2015-12 / 1/2018的时间序列,行数为7/1 / 2015-2 / 1/2016。
我正在创建一个新的表格--- 第一行将是前两行的差异。每行将包含9/1 / 2015-2 / 1/2016的列值之和。 对于第2行,计算将是第2行和第3行的差异。每行将包含10/1 / 2015-3 / 1/2016的列值总和。 对于第3行,它将是第3行和第4行的差异。每行将包含11/1 / 2015-4 / 1/2016的列值之和。
Column Row values
8/1/2015 7/1/2015 25
9/1/2015 7/1/2015 20
10/1/2015 7/1/2015 22
11/1/2015 7/1/2015 13
12/1/2015 7/1/2015 16
1/1/2016 7/1/2015 18
2/1/2016 7/1/2015 10
3/1/2016 7/1/2015 23
4/1/2016 7/1/2015 14
5/1/2016 7/1/2015 32
6/1/2016 7/1/2015 17
7/1/2016 7/1/2015 11
8/1/2016 7/1/2015 12
9/1/2016 7/1/2015 19
10/1/2016 7/1/2015 20
11/1/2016 7/1/2015 33
12/1/2016 7/1/2015 20
8/1/2015 8/1/2015 14
9/1/2015 8/1/2015 25
10/1/2015 8/1/2015 19
11/1/2015 8/1/2015 18
12/1/2015 8/1/2015 22
1/1/2016 8/1/2015 20
2/1/2016 8/1/2015 17
3/1/2016 8/1/2015 18
4/1/2016 8/1/2015 25
5/1/2016 8/1/2015 23
6/1/2016 8/1/2015 28
7/1/2016 8/1/2015 22
8/1/2016 8/1/2015 20
9/1/2016 8/1/2015 24
10/1/2016 8/1/2015 31
11/1/2016 8/1/2015 27
12/1/2016 8/1/2015 24
同样,计算需要从3/1 / 2016-8 / 1/2016等继续进行。
由于
我使用过的代码
newdata2_reshape <- reshape(newdata2,
timevar = c("Column"),
idvar = c("Row"),
direction = "wide")
newdata2_reshape$date<-newdata2_reshape$Row[1]
newdata2_reshape$diff_in_days<- difftime
(newdata2_reshape$Row ,newdata2_reshape$date , units = c("days"))
newdata2_reshape$months<-ifelse(newdata2_reshape$diff_in_days >=28 &
newdata2_reshape$diff_in_days <59,1,
ifelse(newdata2_reshape$diff_in_days>=59 &
newdata2_reshape$diff_in_days<89,2,
ifelse(newdata2_reshape$diff_in_days>=89
& newdata2_reshape$diff_in_days<120,3,
ifelse(newdata2_reshape$diff_in_days>=120 &
newdata2_reshape$diff_in_days<150,4,
ifelse (newdata2_reshape$diff_in_days>=150 &
newdata2_reshape$diff_in_days<181,5,
ifelse(newdata2_reshape$diff_in_days>=181 & newdata2_reshape$diff_in_days<212,6,
ifelse(newdata2_reshape$diff_in_days>=212 & newdata2_reshape$diff_in_days<242,7,
ifelse(newdata2_reshape$diff_in_days>=242 & newdata2_reshape$diff_in_days<273,8,
ifelse(newdata2_reshape$diff_in_days>=273 & newdata2_reshape$diff_in_days<303,9,
ifelse(newdata2_reshape$diff_in_days>=303 newdata2_reshape$diff_in_days<334,10,
ifelse(newdata2_reshape$diff_in_days>=334 newdata2_reshape$diff_in_days <365,11,
ifelse (newdata2_reshape$diff_in_days>=365,12,0))))))))))))
Now I have values against diff in days from 1 to 7 in my data
因此,如果我的值为1,我应该做第2到第7列的总和并从前一行的第2到第7列的总和中减去它
如果我的日期值是2那么我应该做第3到第8列的总和,并从前一行的第3到第8列的总和中减去它。