From the course: R for Data Science: Lunch Break Lessons

cumsum, cumprod, cummax, an dcummin

- [Instructor] R provides cumulative operations for sum, product, minimum and maximum. Let's take a minute to examine how to use them, and why you'd want to use these at all. First of all, there's a simple sum and in line four, you can see that I am summing one plus two plus three. Well that equals six. Now on line seven I've done the same argument, but I'm using cumulative sum. And when I run that, you can see that I get one, three and six. What it's doing is taking the first value, which is one, and then it takes the second value, which in this case is two, so it adds one plus two and it comes up with three. And then it takes the third value, and adds one plus two plus three, which equals six. So that's a cumulative summation. Now one note as we go along, cumulative sum wants one argument. And line nine looks like it should work fine. But it actually is three arguments, one and two and three. So be aware of that common error. Now line 11 exhibit something interesting. You can see that I've inserted an NA. And if I run this what you'll see is that well, it starts off just like you'd expect. One, and then one plus two, and then one plus two plus three, and then it hits the NA. And you'll notice that no further values if result from cumulative summation. NA interrupts the cumulative aspects of this command. In line 15, I've set up a cumulative product on the values three, four and five. So the first value that results is three, which is three times one equals three, and then it takes the second two values three times four equals 12. And then the third three values, three times four times five, which equals six. Likewise, in line 18, I've set up the cumulative minimum. And when I run that, you can see that well, the minimum of one is one, and the minimum of one or two is one, and the minimum of one or two or three is still one. Compare that to cumulative max, where the maximum of one is one, and the maximum of one and two is two and the maximum of one and two and three is three. And you can see that it gives us each of those values as the steps through. In line 21 I've just provided a bit more complex argument. One two three and two, and as you might expect, three continues to be the maximum value even if you add a second two. In line 22 I've added an NA, which is not available. And just as above, once an NA appears in the argument, it prohibits further evaluation. The most common use for cumulative operations such as cumulative sum or cumulative product, is to see the evolving relationship between numbers. So in line 25 through 34, I've set up a plot that will do this. Let's go ahead and step through those lines. Into the graph on the right you can see how cumulative sum has been graphed out against cumulative max. The points are the original vector that I created. Numbers one through 20. So in summary, R provides cumulative operations for minimum, maximum ,sum and product. You'll often see these functions used to illustrate the progress of different values as they go through those different operations.

Contents