使用月份名称在数据框中创建新近度列

时间:2017-09-17 15:08:08

标签: r

我已经将此数据框称为"合并"

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

我已经创建了另一个数据框,我想为每个客户添加第一个和最后一个购买日期(假设当前日期),但我在使用月份名称时遇到了问题。

我已经想出了这个,但它不起作用:

CAMPAIGN     NAME
January      Anderson
February     Anderson
January      Barrett
September    West

我想创建像这样的东西

RFM <- cbind(RFM, FirstPurchase = with(merged, as.integer(by(CAMPAIGN, NAME, min))))

1 个答案:

答案 0 :(得分:0)

让我们说df的内容是

CAMPAIGN     NAME
January      Anderson
February     Anderson
January      Barrett
September    West

然后

library(lubridate)
library(dplyr)

#considering that you only have the same year data, otherwise you may need to consider year as well in your data
df$CAMPAIGN <- dmy(paste("01-", df$CAMPAIGN, "-2017" , sep =""))

df %>% group_by(NAME) %>% 
  summarise(FIRSTPURCHASE=month(min(CAMPAIGN), label=T), LASTPURCHASE=month(max(CAMPAIGN), label=T))


希望这有帮助!