title: How to loop over rolling pairs of columns in dataframe
body: <p>I have a dataframe like this:</p>
  
  <pre><code>set.seed(123)
  df &lt;- data.frame(v1 = sample(1:4, 100, replace = T),
                   v2 = sample(1:4, 100, replace = T),
                   v3 = sample(1:4, 100, replace = T),
                   v4 = sample(1:4, 100, replace = T),
                   v5 = sample(1:4, 100, replace = T))
  </code></pre>
  
  <p>and I want to paste together rolling pairs of columns: <code>v1</code>and <code>v2</code>, <code>v2</code>and <code>v3</code>, <code>v3</code> and <code>v4</code>, and so on. I tried doing this with <code>apply</code> within a <code>for</code> loop:</p>
  
  <pre><code>pairs &lt;- c()
  for(i in 1:ncol(df)){
    pairs &lt;- apply(df[,c(i,i+1)], 1, paste0, collapse = "-")
  }
  </code></pre>
  
  <p>but this throws an error:</p>
  
  <pre><code>Error in `[.data.frame`(df, , c(i, i + 1)) : undefined columns selected
  </code></pre>
  
  <p>What's wrong with the loop? And how could the pairing be achieved? </p>