For a tidy
approach
library(dplyr)
library(tidyr)
library(tibble)
mydf %>%
rownames_to_column() %>%
group_by(rowname, transaction, quality, brand) %>%
summarise(count = n()) %>%
spread(brand, count, sep = "-", fill = 0) %>%
ungroup() %>%
select(-rowname)
# # A tibble: 7 x 5
# transaction quality `brand-1` `brand-2` `brand-3`
# *
# 1 1 NEW 1 0 0
# 2 0 OLD 0 1 0
# 3 1 OLD 0 0 1
# 4 1 OLD 1 0 0
# 5 1 OLD 0 1 0
# 6 0 NEW 0 1 0
# 7 0 NEW 1 0 0