title: How to loop through columns, check if a particular value exists in any of the columns, mutate a new column and enter 1 if it exists, 0 if not?
body: <p>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. </p>
  
  <p>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. </p>
  
  <p>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. </p>
  
  <p>The brute-force code looks something like: </p>
  
  <pre><code>df %&gt;%
    mutate(Q3 == ifelse(col1 == "Q3" | col2 == "Q3" | col3 == "Q3", 0, 1))
  </code></pre>
  
  <p>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. </p>
  
  <p>With 25 questions, the code becomes too long.</p>
  
  <p>EDIT: A sample of the dataframe looks like this. </p>
  
  <pre><code>sample &lt;- "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 "
  </code></pre>
  
  <p>The desired output is as follows: </p>
  
  <pre><code>sample &lt;- "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 "
  </code></pre>