Discussion:
[R-sig-hpc] Automatically create dummy variables for factor, but created by group
Alicia Ellis
2017-02-14 17:24:38 UTC
Permalink
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]]
romunov
2017-02-14 18:02:01 UTC
Permalink
Something like this?

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


Cheers,
Roman
Post by Alicia Ellis
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
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]]
_______________________________________________
R-sig-hpc mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-hpc
--
In God we trust, all others bring data.

[[alternative HTML version deleted]]
Loading...