Note that there are some explanatory texts on larger screens.

plurals
  1. POInterleave n columns of two data frames in R
    text
    copied!<p>I have a dataframe such as:</p> <pre><code>lat lon var01 var02 var03 var04 var11 var12 var13 var14 ... </code></pre> <p>and another one like:</p> <pre><code>lat lon var05 var15 var25 ... </code></pre> <p>The required output is:</p> <pre><code>lat lon var01 var02 var03 var04 var05 var11 var12 var13 var14 var15 ... </code></pre> <p>I thought this would be easy in R, but I haven't found any way so far. I'm also open to solutions in other languages like bash. I would also like to have only a few lines of code, I know how to do it with loops and such.</p> <p>Thanks in advance</p> <p>Edit: The following solution requires that the columns are named correctly. Imagine the following situation:</p> <pre><code>d1 &lt;- data.frame(lat = 1:10, lon = 1:10, V11 = runif(10), V12 = rnorm(10), V21 = runif(10), V22 = rnorm(10)) d2 &lt;- data.frame(lat = 1:10, lon = 1:10, A13 = runif(10), A23 = rnorm(10)) res &lt;- merge(d1, d2, sort = FALSE) res &lt;- res[, c(1:2, order(colnames(res[, -(1:2)])) + 2)] </code></pre> <p>The output is </p> <pre><code>lat lon A13 A23 V11 V12 V21 V22 10 10 0.21269952 0.2670988 0.87532133 -0.6887557 0.60493329 -0.1350546 1 1 0.61464497 -0.5686687 0.91287592 -0.4149946 0.23962942 0.3981059 2 2 0.55715954 -0.1351786 0.29360337 -0.3942900 0.05893438 -0.6120264 3 3 0.32877732 1.1780870 0.45906573 -0.0593134 0.64228826 0.3411197 4 4 0.45313145 -1.5235668 0.33239467 1.1000254 0.87626921 -1.1293631 5 5 0.50044097 0.5939462 0.65087047 0.7631757 0.77891468 1.4330237 6 6 0.18086636 0.3329504 0.25801678 -0.1645236 0.79730883 1.9803999 7 7 0.52963060 1.0630998 0.47854525 -0.2533617 0.45527445 -0.3672215 8 8 0.07527575 -0.3041839 0.76631067 0.6969634 0.41008408 -1.0441346 9 9 0.27775593 0.3700188 0.08424691 0.5566632 0.81087024 0.5697196 </code></pre> <p>and the required output is:</p> <pre><code>lat lon V11 V12 A13 V21 V22 A13 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload