首页 > 解决方案 > 按日期和潜在客户的行动重新排列我的数据集

问题描述

我正在以这种方式构建的数据库上研究 R:

Lead      Date          state
1         01/01/2020    active
1         02/01/2020    standby
2         01/01/2020    standby
2         02/01/2020    active
2         03/01/2020    offer won
3         01/01/2020    standby
3         02/01/2020    active
...       ...           ...

而且我想要类似的东西,使用日期来安排潜在客户的状态:

Lead     Chronology by date
1        active;standby
2        standby;active;offer won
3        standby;active
...      ...

标签: rstring

解决方案


假设您的数据是一个名为 的 data.frame df,您可以使用tidyverse包轻松完成此操作。

library(dplyr)
library(stringr)
df %>%
  group_by(Lead) %>%
  arrange(Date) %>%
  summarize(Chronology_by_date = str_c(state, collapse = ";"))

推荐阅读