r - How can I create a sum of every n numbers? -


this question has answer here:

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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -