title: R: dataframe wide to long format
body: <p>I'm having trouble converting my date from wide to long format in R.</p>
  <p>This is an exemplary dataframe (mine is much larger, but just for you to understand).</p>
  <pre><code>    CASE   Val1     Val2    Val3    Val4     Age
  1   149       1        3       5       4      22
  2   150       3        2       3       2      30
  3   151       1        2       6       4      34
  4   152       3        4       5       5      27
  5   153       2        3       1       3      29
  6   154       5        5       2       4      20  
  7   155       6        2       5       2      41  
  8   156       3        1       1       1      26  
  9   157       1        1       4       5      21  
  10  158       2        3       5       4      36  
  </code></pre>
  <p>My goal is to have a dataframa, that shows me for each CASE the Age and the values, not in wide format (using multiple columns), but rather long format -&gt; resulting in only three columns. This means that in my new data frame the CASE number will be repeated as many times there are values for it, with the right Age and data (Val1, Val2, Val3 and Val4).</p>
  <p>This is how I want it to look basically (the # is to show where the Val is coming from):</p>
  <pre><code>    CASE   Age     Val           
  1   149     22      1    # (Val1)
  2   149     22      3    # (Val2)  
  3   149     22      5    # (Val3) 
  4   149     22      4    # (Val4)   
  5   150     30      3    # (Val1)       
  6   150     30      2    # (Val2)      
  7   150     30      3    # (Val3)       
  8   150     30      2    # (Val4)     
  9   151     34      1    # (Val1)      
  10  151     34      2    # (Val2)     
  ... and so on
  </code></pre>
  <p>I have tried reshape, melt, gather, pivot_longer and so on, but I can't get R to assemble the Val1, Val2, Val3, Val4 in that order from top to bottom only within three columns.</p>
  <p>Solutions on Github show vectors, that need to be specified beforehand. Do I have to specify each values (Val1, Val2, Val3, Val4) as vectors for it to work? The problem with this is, that my dataframe is much larger and available in a csv. data format, that I have uploaded into R-Studio. I have pages full of colums and rows, how could I extract a column as a vector then (since this is only an exemplary dataframe)? How do I call upon columns, within a big dataframe and only work with a selected number out of them (here CASE, Age and Val1-4).</p>
  <p>Example:</p>
  <pre><code>long &lt;- tidyr::pivot_longer(data, cols = -CASE, 
                      names_to = c('variable', 'system'), names_sep = '_')
  </code></pre>
  <p>What do I have to write in &quot;names_to&quot; within that vector, if Val basically bascially must be one vector consisting of Val1, Val2, Val3 and Val4? I have tried to save Val as a vector...</p>
  <pre><code>Val &lt;- c(Val1, Val2, Val3, Val4) 
  </code></pre>
  <p>and then insert it, but it didn't work. Maybe because Val1-4 are just simple columns within my big dataframe? If so, I could not find a way to extract them as vectors.</p>