I have two data frames:
> df1 <- data.frame(a=3:4, b=1:2)
> df2 <- data.frame(a=5:6, b=7:8)
>
> df1
a b
1 3 1
2 4 2
>
> df2
a b
1 5 7
2 6 8
How can I combine (or zip or interleave) them by rows so the result will be following:
> df_final <- some_magic_function(df1, df2)
> df_final
1 3 1
2 5 7
3 4 2
4 6 8
I've found similar approaches here and there, but it is for vectors:
> a <- c(1, 2, 3)
> b <- c(4, 5, 6)
>
> a
[1] 1 2 3
> b
[1] 4 5 6
>
> as.vector(rbind(a,b))
[1] 1 4 2 5 3 6
>
> z1 <- setNames(c(a,b),rep(seq_along(a),2))
> z1 <- as.vector(z1[order(names(z1))])
> z1
[1] 1 4 2 5 3 6
>
> c( matrix(c(a,b), nrow=2, byrow=TRUE) )
[1] 1 4 2 5 3 6
>
> c(a,b)[ order( c(seq_along(a), seq_along(b)))]
How to do the similar for data frames? Please note I want to interleave rows, not to concatenate data frames and then somehow rearrange it's rows.