从累计值到R中的单个值

时间:2012-11-20 13:45:03

标签: r statistics sample accumulate

sample1 <- c(9,9,24,24,27,27,39,45,54,54,54,57,57,57,57,66,66,69,75,81,90,102,105,108,120,120,123)

我怎么能这样做(9,0,15,0,3,0 ...)等等?这个程序叫什么?

1 个答案:

答案 0 :(得分:5)

diff将会这样做。 (请注意,您需要预先设置0来“拾取”第一步,从0到9。)

diff(c(0, sample1))
#  [1]  9  0 15  0  3  0 12  6  9  0  0  3  0  0  0  9  0  3  6
# [20]  6  9 12  3  3 12  0  3