Menu

Suppose we have the following vector

We can see that the difference between the first component and the second component is 2. Is there a function to automatically calculate? Yes, it’s diff() .

Now let’s try to create a data frame with a date column.

Now let’s try diff()  again.

Now, what if we want to create another column with the result? That can be a bit tricky. Dplyr library doesn’t have an easy way to do that.

However, this post (link) has an elegant way to do it. Thanks to josliber for figuring the way out.