计算单列中连续日期之间的天数

时间:2018-06-05 19:00:58

标签: r date difference

我有以下数据框,其中包含一列日期和一列评估:

dates<-c("2015-01-02","2015-01-10","2016-01-15")
assessments<-c('1','2','3')
dates_dataframe = data.frame(dates, assessments)
dates_dataframe$dates<-as.Date(dates_dataframe$dates)

我想在此数据框中创建另一列,其中包含一次评估与下一次评估之间的天数。

我将如何做到这一点?

3 个答案:

答案 0 :(得分:2)

您可以使用diff

执行此操作
dates_dataframe$days = c(0, diff(dates_dataframe$dates))
dates_dataframe$days2 = c(diff(dates_dataframe$dates), 0)

或与NAs:

dates_dataframe$days3 = c(NA, diff(dates_dataframe$dates))
dates_dataframe$days4 = c(diff(dates_dataframe$dates), NA_character_)

<强>结果:

> dates_dataframe
       dates assessments days    days2 days3    days4
1 2015-01-02           1    0   8 days    NA   8 days
2 2015-01-10           2    8 370 days     8 370 days
3 2016-01-15           3  370   0 days   370  NA days

答案 1 :(得分:2)

另一种方法是使用shift函数:

# create data
dates<-c("2015-01-02","2015-01-10","2016-01-15")
assessments<-c('1','2','3')
df <- data.table(dates, assessments)

# convert to date format
df[, dates := as.Date(dates)]

# shift
df[, next_dates := shift(dates, 1)]

# get difference
df[, difference := abs(next_dates - dates)]

        dates assessments next_dates difference
1: 2015-01-02           1       <NA>    NA days
2: 2015-01-10           2 2015-01-02     8 days
3: 2016-01-15           3 2015-01-10   370 days

答案 2 :(得分:1)

我认为你应该使用useR的答案,但这是另一个答案:

dates<-c("2015-01-02","2015-01-10","2016-01-15")
assessments<-c('1','2','3')
dates_dataframe <- cbind.data.frame(dates, assessments)
dates_dataframe$dates <- as.Date(dates_dataframe$dates)
dates_dataframe$dates_shift = shift(dates_dataframe$dates,1)
dates_dataframe$days <- (dates_dataframe$dates - dates_dataframe$dates_shift)


   dates       assessments dates_shift  days
1 2015-01-02           1        <NA>  NA days
2 2015-01-10           2  2015-01-02   8 days
3 2016-01-15           3  2015-01-10 370 days