r - 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.
解决方案
您可以使用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
推荐阅读
- scala - Scala/Spark 确定外部表的路径
- angular - forkJoin 不适用于 Angular 6 中的 URL 和 POST 对象的动态数组
- javascript - express 和 ejs 中的错误,用于在屏幕上显示数据库中的数据
- android - Flutter闪屏android多张图片
- reactjs - React Native android 构建失败:app:mergeReleaseResources
- sql - EXECUTE 后的 SQL Server 事务计数指示 BEGIN 和 COMMIT 语句的数量不匹配
- xml - How to edit a file in Opencart 3.x with an extension?
- java - Any way to monitor a Java application's Function Flow?
- php - $variable after
没有显示在 php 的 echo 中 - shell - Universal copy command for Windows and Linux