[R] sequential sum
baccts
baccts at hotmail.com
Tue Feb 14 18:04:23 CET 2012
Dear R users,
I am trying to sum number that exist in another vector up to i, then
increment i and repeat.
Sorry. It's hard to explain but basically I am trying to do the following:
test <- c(1,2,3,4);
test2 <- c(3,5,6,7,2,8,8,4,4);
test3 <- c(10,20,30,40);
tmp <- 0;
for (i in 1:length(test)){
tmp[i] <- sum(test3[which(test[1:i] %in% test2)]);
}
so when i = 1, tmp[i] = 0 because test[1]=1 is not in test2 and
when i = 3, tmp[i] = 50 = test3[2] + test3[3] because test[2:3] is in test2
Problem is test has 5000+ entries. How do I do the same thing without the
loop?
Thanks in advance.
--
View this message in context: http://r.789695.n4.nabble.com/sequential-sum-tp4387670p4387670.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list