title: Automated sorting and appending of data
body: <p>I am appending columns sequentially, with from one data frame to another based on a unique sorted order from another column in the data frame. I am trying to automate this process so that the columns are automatically sorted before the new column is appended. Below is an example. Data frame df contains the data on which the data frame is to be sorted. Dataframe df2 comprises the columns to be added to the data frame sequentially.</p>
  <pre><code>df &lt;- data.frame(&quot;ID&quot; = 1:16)
  df$col_A &lt;- c(1,2,3,4,5,6,7,8,8,9,10,11,12,13,14,15)
  df$col_B &lt;- c(10,1,10,1,12,12,12,12,1,14,13,14,16,16,16,16)
  df$col_C &lt;- c(10,12,14,3,10,12,14,16,10,12,14,16,10,5,14,16)
  df$col_D &lt;- c(10,12,14,16,10,12,4,16,10,12,14,16,10,12,14,4)
  df
     ID col_A col_B col_C col_D
  1   1     1    10    10    10
  2   2     2     1    12    12
  3   3     3    10    14    14
  4   4     4     1     3    16
  5   5     5    12    10    10
  6   6     6    12    12    12
  7   7     7    12    14     4
  8   8     8    12    16    16
  9   9     8     1    10    10
  10 10     9    14    12    12
  11 11    10    13    14    14
  12 12    11    14    16    16
  13 13    12    16    10    10
  14 14    13    16     5    12
  15 15    14    16    14    14
  16 16    15    16    16     4
  
  df2 &lt;- data.frame(&quot;ID&quot; = 1:16)
  df2$A_rank &lt;- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
  df2$B_rank &lt;- c(7,9,10,8,11,12,14,13,15,4,5,3,6,2,16,1)
  df2$C_rank &lt;- c(1,12,2,16,3,4,5,15,6,7,9,8,10,11,13,14)
  df2$D_rank &lt;- c(16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
  df2
     ID col_A col_B col_C col_D
  1   1     1    10    10    10
  2   2     2     1    12    12
  3   3     3    10    14    14
  4   4     4     1     3    16
  5   5     5    12    10    10
  6   6     6    12    12    12
  7   7     7    12    14     4
  8   8     8    12    16    16
  9   9     8     1    10    10
  10 10     9    14    12    12
  11 11    10    13    14    14
  12 12    11    14    16    16
  13 13    12    16    10    10
  14 14    13    16     5    12
  15 15    14    16    14    14
  16 16    15    16    16     4
  </code></pre>
  <p>I can do this process manually using the following code, but I am unsure how to implement this so it is carried out automatically across n number of variables.</p>
  <pre><code>df2 &lt;- df2 %&gt;% arrange(A_rank) %&gt;% cbind(col_A) 
  df2 &lt;- df2 %&gt;% arrange(B_rank) %&gt;% cbind(col_B)
  df2 &lt;- df2 %&gt;% arrange(C_rank) %&gt;% cbind(col_C) 
  df2 &lt;- df2 %&gt;% arrange(D_rank) %&gt;% cbind(col_D) 
  df2
     ID A_rank B_rank C_rank D_rank Var_A Var_B Var_C Var_D col_A col_B col_C col_D
  1  16     16      1     14      1    15     1    16     4    15     1    16     4
  2  15     15     16     13      2    14    16    14     4    14    16    14     4
  3  14     14      2     11      3    13     1    14    10    13     1    14    10
  4  13     13      6     10      4    12    12    14    10    12    12    14    10
  5  12     12      3      8      5    11     1    12    10    11     1    12    10
  6  11     11      5      9      6    10    10    12    10    10    10    12    10
  7  10     10      4      7      7     9    10    12    12     9    10    12    12
  8   9      9     15      6      8     8    16    10    12     8    16    10    12
  9   8      8     13     15      9     8    16    16    12     8    16    16    12
  10  7      7     14      5     10     7    16    10    12     7    16    10    12
  11  6      6     12      4     11     6    14    10    14     6    14    10    14
  12  5      5     11      3     12     5    14    10    14     5    14    10    14
  13  4      4      8     16     13     4    12    16    14     4    12    16    14
  14  3      3     10      2     14     3    13     5    16     3    13     5    16
  15  2      2      9     12     15     2    12    14    16     2    12    14    16
  16  1      1      7      1     16     1    12     3    16     1    12     3    16
  </code></pre>
  <p>I realise I can flag the columns to do the sort and the add using the lines of code below, but unsure how to write a function which incorporates these column names and ordered column names into the into the process.</p>
  <pre><code>col_names &lt;- colnames(df %&gt;% select(grep(&quot;col&quot;, colnames(.), value = TRUE)))
  col_names
  [1] &quot;col_A&quot; &quot;col_B&quot; &quot;col_C&quot; &quot;col_D&quot; 
  
  ord_names &lt;- colnames(df2 %&gt;% select(grep(&quot;rank&quot;, colnames(.), value = TRUE)))
  ord_names
  [1] &quot;A_rank&quot; &quot;B_rank&quot; &quot;C_rank&quot; &quot;D_rank&quot;
  </code></pre>