I am looking for a good and quick way to apply some arbitrary function that works on vectors, such as sum , sequentially into a subvector of consecutive elements of K. Here is one simple example that should very clearly illustrate what I want:
v <- c(1, 2, 3, 4, 5, 6, 7, 8) v2 <- myapply(v, sum, group_size=3)
The function should try to process groups of group_size elements of the given vector and apply the function to each group (considering it as a different vector). In this example, the vector v2 obtained as follows: (1 + 2 + 3) = 6, (4 + 5 + 6) = 15, (7 + 8) = 15. In this case, K did not divide N exactly, so the last group was less than K.
If there is a nicer / faster solution that only works if N is a multiple of K, I would also appreciate it.
source share