r - How can I create a sum of every n numbers? -
this question has answer here:
- summing every n points in r 8 answers
i have dataframe column:
1 1 1 1 2 2 2 2 3 3 3 3
and want sum every n numbers (say 4). intended output:
4 8 12
how can this? know how rollsum, wanted know if there function this?
x <- data.frame(value = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)) x$group = rep(1:(nrow(x)/4), each=4) tapply(x$value, x$group, fun = sum)
the hardest part here assigning groups. see ?rep
details there.
Comments
Post a Comment