Seperating several observations of a variable and building a matrix

I have a multiple-response-variable with seven possible observations: "Inhalt", "Arbeit", "Verhindern Koalition", "Ermöglichen Koalition", "Verhindern Kanzlerschaft", "Ermöglichen Kanzlerschaft", "Spitzenpolitiker".

If one chose more than one observation, the answers however are not separated in the data (Data)

My goal is to create a matrix with all possible observations as variables and marked with 1 (yes) and 0 (No). Currently I am using this command:

einzeln_strategisch_2021 <- data.frame(strategisch_2021[, ! colnames (strategisch_2021) %in% "Q12"], model.matrix(~ Q12 - 1, strategisch_2021)) %>% 

This gives me the matrix I want but it does not separate the observations, so now I have a matrix with 20 variables instead of the seven (variables).

I also tried seperate() like this:

separate(Q12, into = c("Inhalt", "Arbeit", "Verhindern Koalition", "Ermöglichen Koalition", "Verhindern Kanzlerschaft", "Ermöglichen Kanzlerschaft", "Spitzenpolitiker"), ";") %>% 

This does separate the observations, but not in the right order and without the matrix.

How do I separate my observations and create a matrix with the possible observations as variables akin to the third picture (Matrix)?

Thank you very much in advance :wink:

I think you would use tidyverse/tidyr's pivot_longer function.
For more detailed support please take a few moments and make a reprex.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.