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
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. [EMAIL PROTECTED] wrote:
Dear R users
I am trying to sum selective elements of a vector but my solution
is
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