Skip to content

Automatically create dummy variables for factor, but created by group

2 messages · Alicia Ellis, Roman Luštrik

#
Assume I have a dataframe with 3 categorical variables:
c("A","A", "B", "C", "D", "E", "E"), LABS = c("P", "Q", "R", "S", "T", "P",
"Q"))
MRN VN LABS
   1  A    P
   1  A    Q
   1  B    R
   2  C    S
   2  D    T
   2  E    P
   2  E    Q

I would like to spread this data frame to the following where dummy
variables are created for "LABS" but grouped by VN like:

 MRN VN LABS dummy_P dummy_Q dummy_R dummy_S dummy_T
   1  A    P       1       1       0       0       0
   1  B    R       0       0       1       0       0
   2  C    S       0       0       0       1       0
   2  D    T       0       0       0       0       1
   2  E    P       1       1       0       0       0

I've been trying to use dplyr and tidyr but haven't found a great
solution.  Suggestions?
#
Something like this?

http://stackoverflow.com/questions/17431524/create-a-binary-indicator-matrix-boolean-matrix-in-r


Cheers,
Roman

On Tue, Feb 14, 2017 at 6:24 PM, Alicia Ellis <alicia.m.ellis at gmail.com>
wrote: