计算自R中每个用户ID的最后一次购买以来的天数

时间:2020-05-25 20:19:04

标签: r

感谢您在计算每个用户ID的最后一次购买的天数方面所提供的帮助。我将日期集附加了预期的目标。

enter image description here

谢谢

1 个答案:

答案 0 :(得分:3)

我们可以按“ USERID”分组,并获取当前日期和过去的“ Datetime”转换后的“日期”列中的difftime

library(lubridate)
library(dplyr)
df1 %>%
    mutate(date = mdy_hm(date)) %>% # convert to Datetime class
    group_by(USERID) %>% #group by USERID
    mutate(numberofdays = as.integer(difftime(date, # take the difference
              lag(date, default = first(date)), unit = 'day')))
# A tibble: 8 x 5
# Groups:   USERID [3]
#     ID date                USERID SALES numberofdays
#  <int> <dttm>               <dbl> <dbl>        <int>
#1     1 2018-11-19 10:36:00    500  1000            0
#2     2 2018-11-19 10:41:00    520  1450            0
#3     3 2018-11-23 10:59:00    500  1390            4
#4     4 2018-11-23 11:12:00    530  1778            0
#5     5 2018-11-29 11:52:00    530  1966            6
#6     6 2018-12-05 12:23:00    520  1100           16
#7     7 2018-12-19 12:24:00    520   700           14
#8     8 2018-12-25 21:24:00    520   900            6

数据

df1 <- structure(list(ID = 1:8, date = c("11/19/2018 10:36", "11/19/2018 10:41", 
"11/23/2018 10:59", "11/23/2018 11:12", "11/29/2018 11:52", "12/5/2018 12:23", 
"12/19/2018 12:24", "12/25/2018 21:24"), USERID = c(500, 520, 
500, 530, 530, 520, 520, 520), SALES = c(1000, 1450, 1390, 1778, 
1966, 1100, 700, 900)), class = "data.frame", row.names = c(NA, 
-8L))