Вы можете использовать комбинацию tidyverse
пакетов (dplyr
& tidyr
) и lubridate
примерно так:
library(tidyverse)
library(lubridate)
data <- read.table(text = "2015-9-8,salad
2015-8-30,cookies,cheese,salad,bread
2015-11-21,bread
2015-12-2,vine
2015-3-12,cookies,bread,milk",
header = F)
data %>%
separate(V1, into = c("date", letters[1:5]), sep = ",") %>%
gather(key, item, -date, na.rm = TRUE) %>%
select(-key) %>%
mutate(date = ymd(date),
month = floor_date(date, unit = "month")) %>%
group_by(month, item) %>%
count() %>%
spread(item, n, fill = 0)
## A tibble: 5 x 7
## Groups: month [5]
# month bread cheese cookies milk salad vine
# <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 2015-03-01 1 0 1 1 0 0
# 2 2015-08-01 1 1 1 0 1 0
# 3 2015-09-01 0 0 0 0 1 0
# 4 2015-11-01 1 0 0 0 0 0
# 5 2015-12-01 0 0 0 0 0 1