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

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

diff

diff

- [Instructor] Diff is a utility, best used with time series data, but it can be used with any sequential vector information. It returns a lagged and iterated differences between the elements of a vector. Now that's kind of a confusing phrase. So let's use some plots and graphs to show you exactly what's happening when diff operates on a vector. First of all, I've created a vector. We're calling it Vector one, and you can see that it contains a sequence of numbers; two, four, three, six, five, 10, nine, 18. Let's dive right in and find out what diff does with that vector. So I'll type in diff, D-I-F-F. I want to find the diff in Vector one, and I'm going to lag by two. I'll run that, and you can see that what I receive is one, two, two, four, four, eight. Now how these numbers are created is a result of a somewhat complex formula. And I've worked that out for you in the example file. If you're curious, I would…

Contents