r - Events in last 21 days for every row by Name -


this dataframe looks like. 2 rightmost columns desired columns.these 2 columns check condition whether in last 21 days there "email" activitytype , whether in last 21 days there "webinar" activitytype.

 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 

based on here: extracting event types last 21 day window tried:

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, = .eachi] 

to no avail trues rows "sale". example, second row activitytype = webinar, both email21 & webinar21 should true. when defining last 21 days, trying include day when event happened too.

how this?

using rolling joins 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 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -