Creating new variable with the difference in days with the previous observation?

StackOverflow https://stackoverflow.com/questions/19502674

  •  01-07-2022
  •  | 
  •  

Question

I have a problem calculating the difference in days with the previous observation in a variable and putting that difference in a new variable. The timestamps are stored in a data frame (df) in a variable (date) as year-month-day, like:

date
1993-05-05
1993-05-14
1993-06-27
1993-06-27
1993-07-10
1993-07-27
1993-08-23
1993-09-04

I would like to create a new variable (df$days.prev) with the differences. The intended output should look like:

date        day.prev
1993-05-05
1993-05-14  9
1993-06-27  44
1993-06-27  0
1993-07-10  13
1993-07-27  17
1993-08-23  27
1993-09-04  12

I tried to do this with

df$days.prev <- diff(df$date,lag=1,difference=1)

but got an error message:

Error in $<-.data.frame (*tmp*, "days.prev", value = c(353, 718, 441,  : 
replacement has 990 rows, data has 991

The problem seems obvious to me: for the first observation there is no previous observation, so no difference can be calculated. How do I solve this problem?

Was it helpful?

Solution

Try this:

> transform(df, day.prev= c(NA,diff(as.Date(df[,1]))))
          V1 day.prev
1 1993-05-05       NA
2 1993-05-14        9
3 1993-06-27       44
4 1993-06-27        0
5 1993-07-10       13
6 1993-07-27       17
7 1993-08-23       27
8 1993-09-04       12

Or

df$days.prev <- c(NA,diff(as.Date(df[,1])))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top