For example, I have this data:
data <- data.frame(person=paste0("person_", 1:5),
keyword=sapply(1:5, function(x) paste0(sample(letters, sample(1:5, 1)), collapse = ","))
)
> data
person keyword
1 person_1 k,f,p,w
2 person_2 y,j
3 person_3 y,r
4 person_4 g,w
5 person_5 u,x,c,n
I would like to split the keywords into multiple columns and eventually convert them to binary data, like this:
person k f p w y j r g w u x c n
1 person_1 1 1 1 1 0 0 0 0 0 0 0 0 0
2 person_2 0 0 0 0 1 1 0 0 0 0 0 0 0
3 person_3 0 0 0 0 1 0 1 0 0 0 0 0 0
4 person_4 0 0 0 0 0 0 0 1 1 0 0 0 0
5 person_5 0 0 0 0 0 0 0 0 0 1 1 1 1
What is the best way to achieve this?
Thank you.