I thought of an interesting solution to this problem, which is probably a little strange, but I like it:
as.list(diff(c(0,cumsum(a)[cumsum(b)])));
#
#
#
#
#
#
#
#
#
Description
cumsum(). : , cumsum() (, sum(), ), unlist() cumsum(), @thelatemail, , !
cumsum(a);
#
, , cumsum(b):
cumsum(b);
#
cumsum(a)[cumsum(b)];
#
, diff() :
diff(c(0,cumsum(a)[cumsum(b)]));
## [1] 19 9 2
, , , as.list():
as.list(diff(c(0,cumsum(a)[cumsum(b)])));
#
#
#
#
#
#
#
#
#
lightsnail <- function() { p<-rep(1:length(b),b); as.list(sapply(1:length(b), function(x) {sum(as.numeric(a)[which(p==x)])})); };
thelatemail <- function() as.list(tapply(unlist(a), rep(seq_along(b), b), sum));
psidom <- function() lapply(split(unlist(a), rep(seq_along(b), b)), sum);
tfc <- function() as.list(aggregate(unlist(a), list(rep(1:length(b),b)), sum)[["x"]]);
user20650 <- function() as.list(rowsum(unlist(a), rep(seq_along(b), b), reorder=FALSE));
bgoldst <- function() as.list(diff(c(0,cumsum(a)[cumsum(b)])));
expected <- list(19,9,2);
identical(expected,lightsnail());
identical(expected,unname(thelatemail()));
identical(expected,unname(psidom()));
identical(expected,tfc());
identical(expected,user20650());
identical(expected,bgoldst());
library(microbenchmark);
microbenchmark(lightsnail(),thelatemail(),psidom(),tfc(),user20650(),bgoldst(),times=1e3L);