I have the following data.table with R
library(data.table)
dt = data.table(ID = c("person1", "person1", "person1", "person2", "person2", "person2", "person2", "person2", ...), category = c("red", "red", "blue", "red", "red", "blue", "green", "green", ...))
dt
ID category
person1 red
person1 red
person1 blue
person2 red
person2 red
person2 blue
person2 green
person2 green
person3 blue
....
I am looking how to create a "frequency" of the categorical variables red
, blue
, green
for each unique ID, and then expand these columns to record the counts for each. The resulting data.table would look like this:
dt
ID red blue green
person1 2 1 0
person2 2 1 2
...
I incorrectly thought the correct way to start this with data.table
would be to compute the table()
by group, e.g.
dt[, counts :=table(category), by=ID]
But this appears to count the total number of categorical values by group ID. This also doesn't solve my problem of "expanding" the data.table.
What is the correct way to do this?