I'm looking to dummify a character column. Suppose you have a data frame like this one:
test_data <- data.frame("Emotion" = c("Happy",
"Happy, Sad",
"Sad, Angry, Nervous",
"Happy, Nervous",
"Happy", "Angry",
"Sad, Angry", "Happy",
"Happy, Sad, Angry, Nervous",
"Angry, Nervous",
"Sad, Nervous",
"Sad, Angry, Nervous",
"Happy, Angry",
"Happy, Angry, Nervous",
"Sad, Angry, Nervous",
"Happy, Sad, Angry, Nervous",
"Angry, Nervous"))
And I want to turn it to this:
Happy Sad Angry Nervous
1 0 0 0
1 1 0 0
0 1 1 1
1 0 0 1
1 0 0 0
0 0 1 0
1 1 1 0
1 0 0 0
1 1 1 1
0 0 1 1
0 1 0 1
0 1 1 1
1 0 1 1
1 0 1 1
0 1 1 1
1 1 1 1
0 0 1 1
My previous post got closed and I was pointed to this post. However, the answers in that post don't work for me as they seem to presume that only two emotions appear in each row.
For example, in the third row we have an entry "Sad, Angry, Nervous" which is split into dummy variables "Sad"
and "Angry, Nervous"
instead of being split into dummy variables "Sad"
, "Angry"
and "Nervous"
.
Also, some of the answers in that post which use tidyverse
packages seem to create additional rows in my data which I don't need. I need to keep the same number of rows and just create additional columns with dummy variables. Any help will be greatly appreciated.