1

I have a survey database at user level, one of the fields has several multiple choices that the user has selected. Example

col1 | col2 ID1 | a, b, c ID2 | c, f ID3 | g, k, z

I want to reshape the file as follows using R:

col1| col2(a)| col3(b)| col4(c)| col5(f)| col6(g)| col7(k)| col8(z)** ID1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 ID2 | 0 | 0 | 1 | 1 | 0 | 0 | 0 ID3 | 0 | 0 | 0 | 0 | 1 | 1 | 1

please note: I don't know how many distinct values are existing in the original multiple choice field.

Thanks

GNicoletti
  • 192
  • 2
  • 17

1 Answers1

0

One option is mtabuate after splitting the 'col2' by ,

library(qdapTools)
cbind(df1[1], mtabulate(strsplit(df1$col2, ", ")))
akrun
  • 874,273
  • 37
  • 540
  • 662