按姓名划分的每一行最近21天的事件

时间:2015-06-10 15:25:45

标签: r dataframe data.table dplyr

这就是我的数据框架的样子。最右边的两列是我想要的列。这两列检查条件是否在过去21天内是否存在“电子邮件”活动类型以及过去21天是否存在“网络研讨会”活动类型。

 Name      ActivityType     ActivityDate  Email(last21days) Webinar(last21day)**             
John       Email            1/1/2014        TRUE                  NA   
John       Webinar          1/5/2014        TRUE                 TRUE
John       Sale             1/20/2014       TRUE                 TRUE
John       Webinar          3/25/2014       NA                   TRUE
John       Sale             4/1/2014        NA                   TRUE
John       Sale             7/1/2014        NA                   NA
Tom        Email            1/1/2015        TRUE                   NA   
Tom        Webinar          1/5/2015        TRUE                 TRUE
Tom        Sale             1/20/2015      TRUE                 TRUE
Tom        Webinar          3/25/2015       NA                   TRUE
Tom        Sale              4/1/2015        NA                   TRUE
Tom        Sale              7/1/2015       NA                   NA

基于此处的帮助: Extracting event types from last 21 day window 我试过了:

df$ActivityDate <- as.Date(df$ActivityDate)
library(data.table)
setDT(df)
setkey(df, Name,ActivityDate)
Elsetemp <- df[, .(Name, ActivityDate, ActivityType)]
df[Elsetemp, `:=`(Email21 = as.logical(which(i.ActivityType == "Email")), 
                        Webinar21 = as.logical(which(i.ActivityType == "Webinar"))), 
         roll = -21, by = .EACHI]

无济于事,因为我只获得带有“Sale”的行的TRUE个。例如,第二行,其中ActivityType = Webinar,Email21&amp;网络研讨会21应该说是真的。当我定义最近21天时,我试图将事件发生的那天包括在内。

2 个答案:

答案 0 :(得分:4)

这个怎么样?

使用data.table中的滚动连接

require(data.table)
dt[, ActivityDate := as.Date(ActivityDate, format="%m/%d/%Y")]
setkey(dt, Name, ActivityDate)

roll_index <- function(x, types, roll=21) {
    lapply(types, function(type) {
         idx = x[ActivityType == type][x, roll=roll, which=TRUE]
         as.logical(idx)
    })
}
dt[, c("Email_21", "Webinar_21") := roll_index(dt, c("Email", "Webinar"))]

#     Name ActivityType ActivityDate Email_21 Webinar_21
#  1: John        Email   2014-01-01     TRUE         NA
#  2: John      Webinar   2014-01-05     TRUE       TRUE
#  3: John         Sale   2014-01-20     TRUE       TRUE
#  4: John      Webinar   2014-03-25       NA       TRUE
#  5: John         Sale   2014-04-01       NA       TRUE
#  6: John         Sale   2014-07-01       NA         NA
#  7:  Tom        Email   2015-01-01     TRUE         NA
#  8:  Tom      Webinar   2015-01-05     TRUE       TRUE
#  9:  Tom         Sale   2015-01-20     TRUE       TRUE
# 10:  Tom      Webinar   2015-03-25       NA       TRUE
# 11:  Tom         Sale   2015-04-01       NA       TRUE
# 12:  Tom         Sale   2015-07-01       NA         NA

答案 1 :(得分:0)

基础R解决方案:

Master

首先,我们在没有示例输出的情况下对前三列进行子集化。然后使用#New type of sequence function that can accept vectors seq2 <- function(v1) { res <- list() for(i in seq_along(v1)) { res[[i]] <- seq(v1[i], v1[i]+21, by='day') } as.Date(unlist(res), origin='1970-01-01') } df <- df[ ,1:3] df$ActivityDate <- as.Date(df$ActivityDate, format='%m/%d/%Y') #Email column emailed <- df[df$ActivityType == 'Email', 'ActivityDate'] df$Email <- df$ActivityDate %in% seq2(emailed) #Webinar column webbed <- df[df$ActivityType == 'Webinar', 'ActivityDate'] df$Webinar <- df$ActivityDate %in% seq2(webbed) 转换日期因子。向量as.Date使用emailed字符串查找ActivityType。创建函数Email以查找日期和21天后的日期。它创建了一个可以检查的序列。

seq2

数据

df
#    Name ActivityType ActivityDate Email Webinar
# 1  John        Email   2014-01-01  TRUE   FALSE
# 2  John      Webinar   2014-01-05  TRUE    TRUE
# 3  John         Sale   2014-01-20  TRUE    TRUE
# 4  John      Webinar   2014-03-25 FALSE    TRUE
# 5  John         Sale   2014-04-01 FALSE    TRUE
# 6  John         Sale   2014-07-01 FALSE   FALSE
# 7   Tom        Email   2015-01-01  TRUE   FALSE
# 8   Tom      Webinar   2015-01-05  TRUE    TRUE
# 9   Tom         Sale   2015-01-20  TRUE    TRUE
# 10  Tom      Webinar   2015-03-25 FALSE    TRUE
# 11  Tom         Sale   2015-04-01 FALSE    TRUE
# 12  Tom         Sale   2015-07-01 FALSE   FALSE