Repetition structure in R

4

I'm not getting a logic to do the following problem in repetition structure:

I have a vector A with 1 element and another B with 30 elements. I wanted to subtract the vector A from each element of B so that this subtraction was accumulated, for example:

A=50
B=c(7,6,7,6,5,6,7,5,6,7)

#subtracao
50-7=43;

43-6=37;

37-7=30;

Could someone help?

    
asked by anonymous 18.02.2015 / 04:32

1 answer

5

cumsum is the easiest in this problem

> A=50
> B=c(7,6,7,6,5,6,7,5,6,7)
> cumsum(B)
 [1]  7 13 20 26 31 37 44 49 55 62
> A-cumsum(B)
 [1]  43  37  30  24  19  13   6   1  -5 -12
    
18.02.2015 / 06:20