我有一个包含三列的客户端数据集。第一个是我的客户ID,第二个是购买日期,第三个是我与他们联系的日期。
我想知道我的联系是否有效。所以,如果客户端在联系后返回,我想插入一个列为1,如果他没有联系返回则返回零。
所以,1 - >如果客户退回并且新购买日期大于上次联系日期。
0 - >没有退货,联系日期为NA或新购买日期小于过去的联系日期。
我想插入一个新列,对其进行建模。数据集是:
client <- c("A","B","C","D","E","G","C","B","D","J","A","E","E","A","C")
purchase <- as.Date(c("2017-04-02","2017-04-07","2017-04-10", "2017-04-
13","2017-04-17","2017-04-18","2017-04-20","2017-04-22","2017-04-25","2017-
04-27","2017-04-30","2017-05-10","2017-05-12","2017-05-15","2017-05-20"))
contact <- as.Date(c("2017-05-05",NA,"2017-04-11", "2017-04-14",NA,"2017-04-
20","2017-04-21",NA,"2017-04-30",NA,"2017-05-30",NA,NA,"2017-05-15",NA))
data <- data.frame(client, purchase, contact)
输出结果为:
data$output <- c(0,0,1,1,0,0,1,0,0,0,1,0,0,0,0)
我试过这个:
data$return <- with(data, ave(as.character(client), as.character(client),
sum(client == client, purchase > contact)))
谢谢