title: How to extract values in a diagonal of a data frame in R?
body: <h1>Problem and Data</h1>
  <p>I have following data frame. For each <code>time_frame</code> the corresponding value is in the diagonal. Here's the code to create this data frame:</p>
  <pre><code>&gt; dput(foo)
  structure(list(time_frame = c(20948, 23748, 24026, 24350, 24462
  ), col1 = c(1, 1, 1, 1, 1), col2 = c(2, 2, 2, 2, 2), col3 = c(1, 
  1, 1, 1, 1), col4 = c(3, 3, 3, 3, 3), col5 = c(1, 1, 1, 1, 1)), .Names = c(&quot;time_frame&quot;, 
  &quot;col1&quot;, &quot;col2&quot;, &quot;col3&quot;, &quot;col4&quot;, &quot;col5&quot;), row.names = c(NA, -5L
  ), class = &quot;data.frame&quot;)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/KrgSl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KrgSl.png" alt="enter image description here" /></a></p>
  <h1>Required Output and What I tried</h1>
  <p>My required output is following:</p>
  <pre><code>foo2 &lt;- data.frame(time_frame = c(20948, 23748, 24026, 24350, 24462),
                     turn_signal_state = c(1,2,1,3,1))  
  </code></pre>
  <p>I am not sure what's the best way to get this output. So, I tried <code>tidyr</code> package as follows:</p>
  <pre><code>library(tidyr)
  foo %&gt;% 
    gather(col, turn_signal_state, -time_frame)  
  </code></pre>
  <p>But as you see, this is not exactly what I need. Please guide me what will be the best approach to convert <code>foo</code> to <code>foo2</code>. I prefer using <code>tidyverse</code>. Also, the original file is a text file that I read using <code>read.delim</code>. Please let me know if it is possible to read in the <code>foo2</code> format in the first place. Thanks.</p>