我想计算用户旅程的第一个接触点和用户旅程的最后一个接触点之间以及所有旅程之间的差异。
这是数据CJ
的(简短)示例:
PurchaseID timestamp date
1 2016-03-12 22:18:34 2016-03-12
1 2016-03-13 05:25:49 2016-03-13
2 2015-07-18 13:00:38 2015-07-18
2 2015-08-07 19:16:59 2015-08-07
2 2015-11-03 12:31:35 2015-11-03
...
我想创建一个新变量difference
,它是每个购买ID的第一个日期和最后一个日期之间的差。
以下是我尝试过的并且应该根据本网站上的其他文章进行的工作:
# difference
CJ <- data.table(CJ)
CJ[, difference := max(timestamp) - min(timestamp), by = PurchaseID]
这给出了一个错误:
Error in `[.data.frame`(CJ, , `:=`(diff, max(timestamp) - min(timestamp)), :
unused argument (by = PurchaseID)
当我仅使用变量date
时,会发生相同的错误。
在我的数据子集中,没有发生此错误。到目前为止,我找不到根本原因。有什么想法吗?
此外,dput
> dput(head(CJgroup))
structure(list(UserID = c(9558L, 9558L, 9558L, 9657L, 1L, 1L),
PurchaseID = c(1L, 1L, 1L, 2L, 3L, 4L), timestamp = structure(c(1457817514,
1457843149, 1457868381, 1437217238, 1438967819, 1446550295
), class = c("POSIXct", "POSIXt"), tzone = "Europe/Amsterdam"),
duration = c(5.786, 65.725, 6.492, 57, 120, 459), device = structure(c(2L,
2L, 2L, 1L, 1L, 1L), .Label = c("FIXED", "MOBILE"), class = "factor"),
touchpoint = c(7L, 7L, 7L, 4L, 7L, 1L), purchase_own = c(0L,
0L, 0L, 0L, 0L, 0L), purchase_any = c(0L, 0L, 0L, 0L, 0L,
0L), MobilePanel = c(0L, 0L, 0L, 0L, 0L, 0L), FixedPanel = c(0L,
0L, 0L, 0L, 17L, 17L), CIT = c(0, 0, 0, 0, 0, 0), FIT = c(1,
1, 1, 1, 1, 1), T1 = c(0, 0, 0, 0, 0, 1), T2 = c(0, 0, 0,
0, 0, 0), T3 = c(0, 0, 0, 0, 0, 0), T4 = c(0, 0, 0, 1, 0,
0), T5 = c(0, 0, 0, 0, 0, 0), T6 = c(0, 0, 0, 0, 0, 0), T7 = c(1,
1, 1, 0, 1, 0), T8 = c(0, 0, 0, 0, 0, 0), T9 = c(0, 0, 0,
0, 0, 0), T10 = c(0, 0, 0, 0, 0, 0), T12 = c(0, 0, 0, 0,
0, 0), T13 = c(0, 0, 0, 0, 0, 0), T14 = c(0, 0, 0, 0, 0,
0), T15 = c(0, 0, 0, 0, 0, 0), T16 = c(0, 0, 0, 0, 0, 0),
T18 = c(0, 0, 0, 0, 0, 0), T19 = c(0, 0, 0, 0, 0, 0), T20 = c(0,
0, 0, 0, 0, 0), T21 = c(0, 0, 0, 0, 0, 0), T22 = c(0, 0,
0, 0, 0, 0), devicemobile = c(1, 1, 1, 0, 0, 0), devicefixed = c(0,
0, 0, 1, 1, 1), purchase_comp = c(0, 0, 0, 0, 0, 0), date = structure(c(16872,
16873, 16873, 16634, 16654, 16742), class = "Date"), POS_comp = c(0,
0, 0, 0, 0, 0), POS_own = c(0, 0, 0, 0, 0, 0), CountTP = c(1L,
2L, 3L, 1L, 1L, 1L)), row.names = c(NA, 6L), class = "data.frame")
答案 0 :(得分:0)
这是使用loop %>%
left_join(dict_df, by = "acronym",
suffix = c("_loop", "_dict"))
#> acronym profession_loop profession_dict
#> 1 cmr chinese medical practitioner chinese medical practitioner
#> 2 cmr chinese medical practitioner chinese medical practitioner
#> 3 den dentist dentist
#> 4 den dentist dentist
#> 5 nmw medical practitioner nurse
#> 6 nmw nurse nurse
软件包而不是dplyr
的解决方案。
然后您可以执行以下操作
data.table