Repeating structure in R

Asked

Viewed 332 times

4

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

I have an A vector with 1 element and another B with 30 elements. I wanted to subtract vector A from each element of B so that this subtraction would be 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?

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.