I am working on a research project, and one of the tables is entered in a way that is not quite suitable for analysis yet, so I am trying to reorganize it. Currently, each row is a test-taker, and each column is a question they have answered wrongly, entered in ascending order. So, for the first row, entries may read "Q1" "Q3" "Q9" etc. for the first, second and third columns respectively. There are 25 questions in total.
My goal is to reorganize the data such that there is a column for every question. If a test-taker has answered the question correctly, the entry for the respective column reads 1, and 0 if otherwise.
There is a brute-force way that appears to work. It is possible to mutate each column separately and check for each value in each column. However, there are 25 questions, and all this typing seems extremely inefficient, so I suspect there must be a better way.
The brute-force code looks something like:
df %>%
mutate(Q3 == ifelse(col1 == "Q3" | col2 == "Q3" | col3 == "Q3", 0, 1))
Here, col1, col2, col3 are all columns that might contain Q3, which might be a question the test-taker got wrong. If any of them do, we enter 0. Else, we enter 1.
With 25 questions, the code becomes too long.
EDIT: A sample of the dataframe looks like this.
sample <- "ID Col1 Col2 Col3 Col4
1 100 Q1
2 101 Q3 Q4
3 102 Q2 Q3 Q4
4 103
5 104 Q4
6 105 Q1 Q2 Q3 Q4 "
The desired output is as follows:
sample <- "ID Q1 Q2 Q3 Q4
1 100 0 1 1 1
2 101 1 1 0 0
3 102 1 0 0 0
4 103 1 1 1 1
5 104 1 1 1 0
6 105 0 0 0 0 "