I am trying to prepare survey data for analysis. I have a question with "check all that apply" answers which are sitting in a single column in my data frame with the responses comma separated, ex. below:
|col1|col2|col3|
|123|Yes|Happy, Sad|
|1234|Yes|Sad|
|12345|Yes|NA|
|12346|Yes|Mad, Sad|
This is what I want it to look like:
|col1|col2|col3 = Happy|col4 = Sad|col5 = Mad|
|123|Yes|1|1|0|
|1234|Yes|0|1|0|
|12345|Yes|0|0|0|
|12346|Yes|0|1|1|
I have found multiple postings and guidance on subsetting and separating the comma separated values but not on how to pivot the options to be the name of the columns and assign the 1 or 0 based on whether that selection was made by the respondent. Would love pointers. Thank you in advance!
You're asking for what's commonly known as a model matrix or design matrix. Type ?model.matrix into your R command line to get some instructions about how to create them.