簡體   English   中英

計算 R 中每個用戶 ID 自上次購買以來的天數

[英]Calculate the number of days since the last purchase per user ID in R

感謝您幫助計算每個用戶 ID 上次購買的天數。 我附上了帶有預期目標的日期集。

在此處輸入圖像描述

謝謝,

我們可以按 'USERID' 分組並獲取當前和過去的 'Datetime' 轉換的 'date' 列的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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM