Alicia Ellis
2017-02-14 17:24:38 UTC
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 LABS1 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]]