首页 > 解决方案 > Calculating Interpurchase time with the first transaction subtracted from a reference date

问题描述

I have the following dataframe

id <- c(1,1,2,2)
date <- c("2011-01-18","2011-01-31","2011-01-02","2011-01-15")
df <- data.frame(id,date)

id  date
1 2011-01-18
1 2011-01-31
2 2011-01-02
2 2011-01-15

Now, i would like to calculate the inter-purchase time of the first transaction with a reference date (2011-01-01) and the second transaction is the difference between second and first. So i get the following result

id  date     interpurchase_time
1 2011-01-18  17
1 2011-01-31  13
2 2011-01-02  01
2 2011-01-15  13

Could anyone please help me out with this? I am just a beginner is R

Thanks

P.S- I have looked upto this Calculating Inter-purchase time in R but here the first transaction is considered as zero which i do not want.

标签: r

解决方案


您可以使用lag函数将每个date与前一个进行比较,对于 each id,但是当没有以前的日期时,您可以使用默认日期(在您的情况下为 2011-01-01)。

id <- c(1,1,2,2)
date <- c("2011-01-18","2011-01-31","2011-01-02","2011-01-15")
df <- data.frame(id,date)

library(dplyr)
library(lubridate)

df %>%
  group_by(id) %>%
  mutate(date = ymd(date),
         int_time = as.numeric(date - lag(date, default = ymd("2011-01-01")))) %>%
  ungroup()

# # A tibble: 4 x 3
#      id date       int_time
#   <dbl> <date>        <dbl>
# 1     1 2011-01-18       17
# 2     1 2011-01-31       13
# 3     2 2011-01-02        1
# 4     2 2011-01-15       13

推荐阅读