sequential sum

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

sequential sum

baccts
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.
Reply | Threaded
Open this post in threaded view
|

Re: sequential sum

Nordlund, Dan (DSHS/RDA)
> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of baccts
> Sent: Tuesday, February 14, 2012 9:04 AM
> To: [hidden email]
> Subject: [R] sequential sum
>
> 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.
>

Your example data is not very extensive so I don't know if this solution is general enough.  But, it does work with your data.

tmp <- cumsum(ifelse(test %in% test2, test3, 0))


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: sequential sum

David Winsemius

On Feb 14, 2012, at 1:38 PM, Nordlund, Dan (DSHS/RDA) wrote:

>> -----Original Message-----
>> From: [hidden email] [mailto:r-help-bounces@r-
>> project.org] On Behalf Of baccts
>> Sent: Tuesday, February 14, 2012 9:04 AM
>> To: [hidden email]
>> Subject: [R] sequential sum
>>
>> 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.
>>
>
> Your example data is not very extensive so I don't know if this  
> solution is general enough.  But, it does work with your data.

I had exactly that concern so tested my solution, which was a bit  
different than yours:

test <- c(1,2,3,4,10, 5);
test2 <- c(3,5,6,7,2,8,8,4,4);
test3 <- c(10,20,30,40, 50, 60);
tmp <- 0;
for (i in 1:length(test)){
tmp[i] <- sum(test3[which(test[1:i] %in% test2)]);
}

 > tmp
[1]   0  20  50  90  90 150
 > tmp2 <- ifelse(test %in% test2, cumsum(test3[test %in% test2]), 0)
 > tmp2
[1]   0  50  90 150   0  50  # was going to throw away.


 > tmp3 <- cumsum(ifelse(test %in% test2, test3, 0))
 > tmp3
[1]   0  20  50  90  90 150

So yours is better at spanning the non-match entries in the same  
manner as the for-loop.


>
>
> Hope this is helpful,
>
> Dan
>
> Daniel J. Nordlund


David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: sequential sum

baccts

Dan,
 
Thank you very much. That is exactly what I need. What a clever solution.
 
David, thanks for trying.
 
Thank you all. I would never be able to figure it out on my own.
 
Lin

 

> From: [hidden email]
> To: [hidden email]
> Date: Tue, 14 Feb 2012 14:28:21 -0500
> CC: [hidden email]
> Subject: Re: [R] sequential sum
>
>
> On Feb 14, 2012, at 1:38 PM, Nordlund, Dan (DSHS/RDA) wrote:
>
> >> -----Original Message-----
> >> From: [hidden email] [mailto:r-help-bounces@r-
> >> project.org] On Behalf Of baccts
> >> Sent: Tuesday, February 14, 2012 9:04 AM
> >> To: [hidden email]
> >> Subject: [R] sequential sum
> >>
> >> 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.
> >>
> >
> > Your example data is not very extensive so I don't know if this
> > solution is general enough. But, it does work with your data.
>
> I had exactly that concern so tested my solution, which was a bit
> different than yours:
>
> test <- c(1,2,3,4,10, 5);
> test2 <- c(3,5,6,7,2,8,8,4,4);
> test3 <- c(10,20,30,40, 50, 60);
> tmp <- 0;
> for (i in 1:length(test)){
> tmp[i] <- sum(test3[which(test[1:i] %in% test2)]);
> }
>
> > tmp
> [1] 0 20 50 90 90 150
> > tmp2 <- ifelse(test %in% test2, cumsum(test3[test %in% test2]), 0)
> > tmp2
> [1] 0 50 90 150 0 50 # was going to throw away.
>
>
> > tmp3 <- cumsum(ifelse(test %in% test2, test3, 0))
> > tmp3
> [1] 0 20 50 90 90 150
>
> So yours is better at spanning the non-match entries in the same
> manner as the for-loop.
>
>
> >
> >
> > Hope this is helpful,
> >
> > Dan
> >
> > Daniel J. Nordlund
>
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
     
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.