I have a dataframe such as:
lat lon var01 var02 var03 var04 var11 var12 var13 var14 ...
and another one like:
lat lon var05 var15 var25 ...
The required output is:
lat lon var01 var02 var03 var04 var05 var11 var12 var13 var14 var15 ...
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.
Thanks in advance
Edit: The following solution requires that the columns are named correctly. Imagine the following situation:
d1 <- data.frame(lat = 1:10, lon = 1:10, V11 = runif(10), V12 = rnorm(10), V21 = runif(10), V22 = rnorm(10))
d2 <- data.frame(lat = 1:10, lon = 1:10, A13 = runif(10), A23 = rnorm(10))
res <- merge(d1, d2, sort = FALSE)
res <- res[, c(1:2, order(colnames(res[, -(1:2)])) + 2)]
The output is
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
and the required output is:
lat lon V11 V12 A13 V21 V22 A13
merge()is a suitable tool for this job. Here is an example:Which gives:
Update based on revised Q:
This gives:
OK, so now note the ordering. Assume we want to take variables in groups of 2 from
df3with 1 variable fromdf4and within each ofdf3anddf4the variables are in the correct order within themselves. For this we need to create an index vectorordthat is:which we then add
2too (to cover thelatandloncolumns in the merged data frame)Once you have the sequence, we just need a way to use R’s vectorised tools and a tiny bit of math to produce the sequence we want. I build the index up in two stages; i) first I work out where the columns
(1:6) + 2of the merged data frame should be inord, and then ii) I fill in the remaining spaces with the indexes in the merged data frame of the columns from the second data frame.That gives:
which is the ordering you wanted. Now we can cook that into a little function:
Which when used on
df3anddf4above gives:Which is again what you wanted. You could fiddle with the function definition so you don’t need to specify both
ngrpsandningrpsas you can work one out from the other plus the number of columns indf3– 2. But I’ll leave that as an exercise for the reader.