Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以R为单位计算采购行程长度_R_Time - Fatal编程技术网

以R为单位计算采购行程长度

以R为单位计算采购行程长度,r,time,R,Time,我目前在R中遇到了一个非常具体的问题:我有一个大约250万行的数据集,其中显示了有关购买行程的基于事件的数据。格式如下(为了简单起见,我排除了大多数人口统计数据和一些其他变量): 我想分析每个单独旅程的联系人之间的平均时间如何影响购买概率。因此,我想计算每个客户旅程的总长度(例如,PurchaseID 1的开始时间到PurchaseID 1的结束时间)。之后,我希望聚合数据,使其如下所示: UserID PurchaseID Customer journey length Pu

我目前在R中遇到了一个非常具体的问题:我有一个大约250万行的数据集,其中显示了有关购买行程的基于事件的数据。格式如下(为了简单起见,我排除了大多数人口统计数据和一些其他变量):

我想分析每个单独旅程的联系人之间的平均时间如何影响购买概率。因此,我想计算每个客户旅程的总长度(例如,PurchaseID 1的开始时间到PurchaseID 1的结束时间)。之后,我希望聚合数据,使其如下所示:

   UserID   PurchaseID    Customer journey length  Purchase   Age   
     1          1                 03:49:14            0       35
     1          2                621:38:28            1       35
     2          3                 00:01:40            0       51
我真的不知道从哪里开始,所以我希望你能帮助我!非常感谢

这应该可以完成工作(使用非常小的样本,请测试):

库(dplyr)
图书馆(lubridate)
df%
总结(行程长度=数值形式(difftime(最大(ContactTime)、最小(ContactTime)、单位=“秒”))

请注意,我已经以秒的形式给出了行程长度,这是可以更改的

这里是提供的解决方案的替代方案

dat1 <- aggregate(. ~PurchaseID+UserID, data=df[,1:3], function(V)max(V)-min(V))
dat2 <- aggregate(. ~PurchaseID+UserID, data=df[,c(1:2, 4)], sum)
dat3 <- aggregate(. ~PurchaseID+UserID, data=df[,c(1:2, 5)], mean)

dat <- merge(merge(dat1, dat2, by = c("PurchaseID", "UserID")), 
         dat3, by = c("PurchaseID", "UserID")) 
   )
dat <- dat[-which(dat$TimeofContact == 0),]
# some polishing
names(dat)[3] <- "CustomerJourneyLength"
# converting time differences in a more suitable format
hours <- dat$CustomerJourneyLength %/% 3600
minutes <- (dat$CustomerJourneyLength %% 3600)%/%60
seconds <- (dat$CustomerJourneyLength %% 3600)%%60
dat$CustomerJourneyLength <- paste0(hours, " hours ", minutes, " minutes ", round(seconds), " seconds")

# which yields
> dat
  PurchaseID UserID          CustomerJourneyLength Purchase Age
1          1      1 15 hours 28 minutes 49 seconds        1  27
2          1      2 15 hours 21 minutes 44 seconds        3  31
3          2      1  4 hours 11 minutes 17 seconds        2  27
5          3      1  9 hours 39 minutes 45 seconds        1  27
6          3      2 14 hours 36 minutes 31 seconds        1  31

dat1使用data.table,运行速度很快

library(data.table)
重新创建数据:

dat <-
  data.table(
    UserID = round(runif(1e5, 1, 1e5 / 5)),
    PurchaseID = round(runif(1e5, 1, 5)),
    timeOfContact = as.POSIXct(runif(1e5, 0, 2e5), origin = '2017-09-20'),
    Purchase = round(runif(1e5, 0, 1)),
    age = round(runif(1e5, 15, 65))
  )
dat[, age := max(age), .(UserID)]
dat[, Purchase := max(Purchase), .(UserID, PurchaseID)]

另一方面,请避免使用带有空格的列名。

如果我理解正确,您要做的是按照
UserID
PurchaseID
的唯一组合进行分组,然后获得第一次和最后一次
联系时间之间的差异
?也可以按.numeric(difftime(max(Contactime),min)进行分组(Contactime,units=“secs”)
要将difftime对象转换为秒数,可能会更好,谢谢。更容易与下游合作。
df <- data.frame(UserID = sample(1:2, 20, replace = T), 
             PurchaseID = sample(1:3, 20, replace = T),
             TimeofContact = runif(20, Sys.time(), Sys.time() + 20*3600),
             Purchase = sample(0:1, 20, replace = T), 
             Age = rep(NA, 20))
df$Age[which(df$UserID == 1)] <- sample(20:40, 1)
df$Age[which(df$UserID == 2)] <- sample(20:40, 1)
library(data.table)
dat <-
  data.table(
    UserID = round(runif(1e5, 1, 1e5 / 5)),
    PurchaseID = round(runif(1e5, 1, 5)),
    timeOfContact = as.POSIXct(runif(1e5, 0, 2e5), origin = '2017-09-20'),
    Purchase = round(runif(1e5, 0, 1)),
    age = round(runif(1e5, 15, 65))
  )
dat[, age := max(age), .(UserID)]
dat[, Purchase := max(Purchase), .(UserID, PurchaseID)]
dat[, .(customerJourneyLength = as.numeric(difftime(
  max(timeOfContact),
  min(timeOfContact),
  tz = 'GMT',
  units = 'secs'
))), .(UserID, PurchaseID, Purchase, age)]