I have two identical data frames. Same column and row headings. One has correlations in it and one has p-values. I want to merge these two tables in an interleaved manner, so that the first column is the correlation, the next the p-value, the next the correlation etc.
So if columns for table 1 are A1, A2, A3... and table 2 are B1, B2, B3... I want the end table to be A1, B1, A2, B2, A3, B3....
I've tried cbind but this puts all the correlation columns first then all the p-value columns. No interleaving.
I'm wondering if there's an easy way.
Thanks for any help!
You got the first step right, which is cbind
ing. Let's say your data frames are d1
with n columns A1, A2, ...,An
and d1
with n columns B1, B2, ..., Bn
. Then d <- cbind(d1, d2)
will give you the data frame containing all the information you wanted, and you just need to re-order its columns. This amounts to generating a vector (1, (n+1), 2, (n+2), ..., n, 2n) to index the data frame columns. You can do this as s <- rep(1:n, each = 2) + (0:1) * n
. So finally, your desired data frame is d[s]
.