我有data.frame
这样:
head(MatrixVertice2011,35)
values ind
5 10.82000 2011-01-03
6 11.75000 2011-01-03
7 10.84048 2011-01-04
8 11.79000 2011-01-04
9 10.87095 2011-01-05
10 11.84000 2011-01-05
11 10.88928 2011-01-06
12 11.88000 2011-01-06
13 10.92000 2011-01-07
14 12.03000 2011-01-07
19 10.93984 2011-01-10
20 12.17000 2011-01-10
...
33 11.10000 2011-01-17
34 12.38000 2011-01-17
35 11.11000 2011-01-18
36 12.32000 2011-01-18
37 11.15000 2011-01-19
38 12.30000 2011-01-19
39 11.14000 2011-01-20
40 12.30000 2011-01-20
41 11.15000 2011-01-21
42 12.35000 2011-01-21
47 11.14000 2011-01-24
48 12.40000 2011-01-24
49 11.14000 2011-01-26
50 12.51000 2011-01-26
51 11.14000 2011-01-27
我需要做滞后差异。我需要根据日期
这样做当我在2011-01-03时,我会区分第6至第5,因此,步骤到下一个日期,依此类推,尊重日期。
你能帮我实现一个代码/功能或提示,这样做吗?
当我这样做时:
MatrixSPREAD<-diff(MatrizVertice2011$values)
这个命令显然不尊重日期。
答案 0 :(得分:1)
使用data.table
:
library(data.table)
setDT(MatrixVertice2011)[, list(diff=diff(values)), by=ind]
ind diff
1: 2011-01-03 0.93000
2: 2011-01-04 0.94952
3: 2011-01-05 0.96905
...
12: 2011-01-24 1.26000
13: 2011-01-26 1.37000
使用dplyr
:
library(dplyr)
MatrixVertice2011 %>% group_by(ind) %>%
summarise(diff = diff(values)[1])
以基地R:
aggregate(values ~ ind, FUN=diff, data=MatrixVertice2011)