计算R中的购买时间

时间:2012-12-08 01:30:36

标签: r

我有以下数据框:

id<-c(1,1,1,3,3,3)
date<-c("23-01-07","27-01-07","30-01-07","11-12-07","12-12-07","01-01-08")
df<-data.frame(id,date)
df$date2<-as.Date(as.character(df$date), format = "%d-%m-%y")


id    date      date2
1 23-01-07 2007-01-23
1 27-01-07 2007-01-27
1 30-01-07 2007-01-30
3 11-12-07 2007-12-11
3 12-12-07 2007-12-12
3 01-01-08 2008-01-01

现在我需要计算每个id的交易的购买时间(同一客户的每次交易与之前交易之间的天数);这样我得到以下结果:

id    date      date2  interpurchase.time
1 23-01-07 2007-01-23         0
1 27-01-07 2007-01-27         4 
1 30-01-07 2007-01-30         3
3 11-12-07 2007-12-11         0  
3 12-12-07 2007-12-12         1 
3 01-01-08 2008-01-01        20

我想知道是否有人可以帮助我。

1 个答案:

答案 0 :(得分:4)

您可以使用plyr

library(plyr)
ddply(df, "id", transform, inter.time = c(0, diff(date2)))

ave

transform(df, inter.time = ave(as.numeric(date2), id,
                               FUN = function(x)c(0, diff(x))))

两者都给予

#   id     date      date2 inter.time
# 1  1 23-01-07 2007-01-23          0
# 2  1 27-01-07 2007-01-27          4
# 3  1 30-01-07 2007-01-30          3
# 4  3 11-12-07 2007-12-11          0
# 5  3 12-12-07 2007-12-12          1
# 6  3 01-01-08 2008-01-01         20

P.S。:您可能希望用NA替换这些零。