apply(ing) to sum subset of a vector

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

apply(ing) to sum subset of a vector

Gary Wessle-2
Dear R users
 
 I am trying to sum selective elements of a vector but my solution
 is not cutting it.
 
 Example:
 > g <- 1:5;
 
 > from <- 1:3;
 > to <- 3:5;
 from to
 1       3
 2       4
 3       5
 
 so I expect 3 sums from g
 1+2+3  that is 1 to 3 of g
 2+3+4  that is 2 to 4 of g
 3+4+5  that is 3 to 5 of g
 
 my solution will not work.
 sum.em <- function(g, c1, c2) sum(g[c1:c2])
 apply(g, 1, sum.em, ...) I don't think so because apply is not
 aware of the from and to. and if I f <- list(g, from, to) that will not fit with the second arg of apply.
 
 thank you
 
                       
---------------------------------

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

Re: apply(ing) to sum subset of a vector

Jacques VESLOT
apply(cbind(from,to), 1, function(x) sum(g[x[1]:x[2]]))

Fred J. a écrit :

>Dear R users
>
> I am trying to sum selective elements of a vector but my solution
> is not cutting it.
>
> Example:
> > g <- 1:5;
>
> > from <- 1:3;
> > to <- 3:5;
> from to
> 1       3
> 2       4
> 3       5
>
> so I expect 3 sums from g
> 1+2+3  that is 1 to 3 of g
> 2+3+4  that is 2 to 4 of g
> 3+4+5  that is 3 to 5 of g
>
> my solution will not work.
> sum.em <- function(g, c1, c2) sum(g[c1:c2])
> apply(g, 1, sum.em, ...) I don't think so because apply is not
> aware of the from and to. and if I f <- list(g, from, to) that will not fit with the second arg of apply.
>
> thank you
>
>
>---------------------------------
>
> [[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
>
>  
>

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

Re: apply(ing) to sum subset of a vector

jholtman
In reply to this post by Gary Wessle-2
create a matrix and then use apply:

> g <- 1:5;
>
>  from <- 1:3;
>  to <- 3:5;
>
> index <- cbind(from,to)
> apply(index, 1, function(x) sum(g[x[1]:x[2]]))
[1]  6  9 12
>



On 3/27/06, Fred J. <[hidden email]> wrote:

>
> Dear R users
>
> I am trying to sum selective elements of a vector but my solution
> is not cutting it.
>
> Example:
> > g <- 1:5;
>
> > from <- 1:3;
> > to <- 3:5;
> from to
> 1       3
> 2       4
> 3       5
>
> so I expect 3 sums from g
> 1+2+3  that is 1 to 3 of g
> 2+3+4  that is 2 to 4 of g
> 3+4+5  that is 3 to 5 of g
>
> my solution will not work.
> sum.em <- function(g, c1, c2) sum(g[c1:c2])
> apply(g, 1, sum.em, ...) I don't think so because apply is not
> aware of the from and to. and if I f <- list(g, from, to) that will not
> fit with the second arg of apply.
>
> thank you
>
>
> ---------------------------------
>
>        [[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
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390 (Cell)
+1 513 247 0281 (Home)

What the problem you are trying to solve?

        [[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