Assuming I have a datatable dt.recipes
which consists of lists with various items, for example:
recipe_id ingredients
1 apple, banana, cucumber, water
2 apple, meat, water
3 water
How can I create a table, counting the amount of unique items present in dt.recipes$ingredients
? In other words, I am looking for a result similar to this:
ingredient count
water 3
apple 2
banana 1
cucumber 1
meat 1
Any pointers would be greatly appreciated, thanks in advance!
A data.table
way could be
library(data.table)
dt[, .(table(unlist(ingredients)))]
# V1 N
#1: apple 2
#2: banana 1
#3: cucumber 1
#4: meat 1
#5: water 3
data
dt <- data.table(
"recipe_id" = 1:3,
"ingredients" = list(
c("apple", "banana", "cucumber", "water"),
c("apple", "meat", "water"),
c("water")
)
)