I have a dataframe with this column:
1
1
1
1
2
2
2
2
3
3
3
3
And I want to sum it every n numbers (Say 4). Intended output:
4
8
12
How can I do this? I know how to do rollsum, but wanted to know if there is any function to do this?
I have a dataframe with this column:
1
1
1
1
2
2
2
2
3
3
3
3
And I want to sum it every n numbers (Say 4). Intended output:
4
8
12
How can I do this? I know how to do rollsum, but wanted to know if there is any function to do 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 is assigning the groups. See ?rep
for details there.