# 计算R中的相对累积和

``````data <- read.table(text="group;  year;    val
a;        1928;    20
a;        1929;    50
a;        1930;    40
a;        1931;    45
b;        1935;   -10
b;        1936;   -15 ", sep=";", header=T, stringsAsFactors = FALSE)

> data
group year val
1                    a 1928  20
2                    a 1929  50
3                    a 1930  40
4                    a 1931  45
5                    b 1935 -10
6                    b 1936 -15
``````

``````               group year  val sum_rel
a     1927      -110
a     1928  20  -90
a     1929  50  -40
a     1930  40    0
a     1931  45   45
b     1934      0
b     1935 -10  -10
b     1936 -15  -25
``````

``````do.call("rbind", unname(lapply(split(data, data\$group), function(x) {
x<-x[order(x\$year),]
cx <- c(which(x\$year==1930),0)[1]+1
cs <- cumsum(c(0, x\$val))
cbind(rbind(transform(x[1,], val=NA, year=min(x\$year)-1), x), sum_rel=cs-cs[cx])
})))

#               group year val sum_rel
# 1                 a 1927  NA    -110
# 2                 a 1928  20     -90
# 3                 a 1929  50     -40
# 4                 a 1930  40       0
# 5                 a 1931  45      45
# 52                b 1934  NA       0
# 51                b 1935 -10     -10
# 6                 b 1936 -15     -25
``````

0条评论