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

romunov romunov at gmail.com
Tue Feb 14 19:02:01 CET 2017


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:

> 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]]
>
> _______________________________________________
> R-sig-hpc mailing list
> R-sig-hpc at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-hpc
>



-- 
In God we trust, all others bring data.

	[[alternative HTML version deleted]]



More information about the R-sig-hpc mailing list