Reputation: 1
I am doing a military mental health survey that has check all that apply questions in it. When a person has multiple answers to one question, how do I split the levels but keep them with the same identifier?
For examply my variable DADT has the levels Before, During, and After. Some people have selected all three choices or two choices. I want to build a mosaic plot comparing DADT to service members gender identity. I the mosiac plot represent three catigories for DADT and not seven catigories.
I would like to continue having the levels split for analys against other variables.
zz <- "ID Branch.Service DADT Birth.Gender Gender.Identity
1 1 Navy,Army Before Yes Male
2 2 Navy After Yes Male
4 3 Marines Before,During,After Yes Male
5 4 Navy After No Non-binary
6 5 Airforce During Yes Female
Data <- read.table(text=zz, header = TRUE)
Upvotes: 0
Views: 177
Reputation: 1305
Is this what you are looking for? It uses the separate_rows function from tidyr to make new rows for each element of a comma separated list of values in a column.
zz <- "ID Branch.Service DADT Birth.Gender Gender.Identity
1 1 Navy,Army Before Yes Male
2 2 Navy After Yes Male
4 3 Marines Before,During,After Yes Male
5 4 Navy After No Non-binary
6 5 Airforce During Yes Female"
Data <- read.table(text=zz, header = TRUE)
Data <- tidyr::separate_rows(Data, Branch.Service, DADT, sep = ",")
ID Branch.Service DADT Birth.Gender Gender.Identity
<int> <chr> <chr> <fct> <fct>
1 1 Navy Before Yes Male
2 1 Army Before Yes Male
3 2 Navy After Yes Male
4 3 Marines Before Yes Male
5 3 Marines During Yes Male
6 3 Marines After Yes Male
7 4 Navy After No Non-binary
8 5 Airforce During Yes Female
Upvotes: 1