[R-sig-hpc] Automatically create dummy variables for factor, but created by group

Alicia Ellis alicia.m.ellis at gmail.com
Tue Feb 14 18:24:38 CET 2017


Assume I have a dataframe with 3 categorical variables:

> df <- data.frame(MRN = c("1", "1", "1", "2", "2", "2", "2"), VN =
c("A","A", "B", "C", "D", "E", "E"), LABS = c("P", "Q", "R", "S", "T", "P",
"Q"))

> df

 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?

	[[alternative HTML version deleted]]



More information about the R-sig-hpc mailing list