0

我有一个数据框,其中的观察结果存储为

> m

    ID       Date count Day Month
1  111 2011-05-22     0 Sun   May
2  111 2011-05-23     5 Mon   May
3  111 2011-05-24     5 Tue   May
4  111 2011-05-25     2 Wed   May
5  111 2011-05-26     2 Thu   May
6  112 2011-05-22     2 Sun   May
7  112 2011-05-23     2 Mon   May
8  112 2011-05-24     1 Tue   May
9  111 2011-05-25     0 Wed   May
10 112 2011-05-26     6 Thu   May

我需要添加几列来添加与 ID 和日期相对应的“计数”变量的过去值。例如,对于 ID 111,我需要一天前、两天前、三天前或每个日期的任何其他时间范围以及每个 ID 对应的计数总和。

所需的输出就像

> m

    ID       Date count Day Month TwoDaysSum ThreeDaysSum
1  111 2011-05-22     0 Sun   May         NA           NA
2  111 2011-05-23     5 Mon   May         NA           NA
3  111 2011-05-24     5 Tue   May          5           NA
4  111 2011-05-25     2 Wed   May         10           10
5  111 2011-05-26     2 Thu   May          7            7
6  112 2011-05-22     2 Sun   May         NA           NA
7  112 2011-05-23     2 Mon   May         NA           NA
8  112 2011-05-24     1 Tue   May          4           NA
9  111 2011-05-25     0 Wed   May          3            5
10 112 2011-05-26     6 Thu   May          1            3

最终,我需要回过头来找到更大时间范围的总和,比如一个月/季度/年。

有人知道如何去做吗?

4

1 回答 1

0
DF<-read.table(text=" ID       Date count Day Month
1  111 2011-05-22     0 Sun   May
2  111 2011-05-23     5 Mon   May
3  111 2011-05-24     5 Tue   May
4  111 2011-05-25     2 Wed   May
5  111 2011-05-26     2 Thu   May
6  112 2011-05-22     2 Sun   May
7  112 2011-05-23     2 Mon   May
8  112 2011-05-24     1 Tue   May
9  112 2011-05-25     0 Wed   May #fixed typo
10 112 2011-05-26     6 Thu   May",header=T,stringsAsFactors = F,sep="")

#sort to be safe
DF<-DF[order(DF$ID,DF$Date),]

DF$TwoDaysSum<-ave(DF$count,DF$ID,FUN=function(x) filter(x,c(0,1,1),sides=1))
DF$ThreeDaysSum<-ave(DF$count,DF$ID,FUN=function(x) filter(x,c(0,1,1,1),sides=1))

DF

    ID       Date count Day Month TwoDaysSum ThreeDaysSum
1  111 2011-05-22     0 Sun   May         NA           NA
2  111 2011-05-23     5 Mon   May         NA           NA
3  111 2011-05-24     5 Tue   May          5           NA
4  111 2011-05-25     2 Wed   May         10           10
5  111 2011-05-26     2 Thu   May          7           12
6  112 2011-05-22     2 Sun   May         NA           NA
7  112 2011-05-23     2 Mon   May         NA           NA
8  112 2011-05-24     1 Tue   May          4           NA
9  112 2011-05-25     0 Wed   May          3            5
10 112 2011-05-26     6 Thu   May          1            3
于 2012-07-20T20:26:02.737 回答