Reputation: 111
Sorry I did not find similar answer, if there were, from past posts here.
Suppose I have two simple n-by-m data frames, df1 and df2. Now I want to combine them to get a n-by-2m data frame called df. By doing this, I want column 1 in df2 to be column 2 in df, column 2 in df2 to be column 4 in df, column 3 in df2 to be column 6 in df....Meanwhile, column 1 in df1 is column 1 in df, column 2 in df1 is column 3 in df...
It means in the new df, column 1, 3, 5, 7...come from df1 and column 2, 4, 6, 8...come from df2.
In general, it looks like to INSERT df2 into df1 by every other column, to put each column of df2 behind its corresponding column in df1.
Can anybody help me on this?
Upvotes: 0
Views: 2310
Reputation: 3711
why does it need to be that way, they are variables after all, but if you want to do it
m<-ncol(df1)
df<-merge(df1,df2)
df<-df[,c(seq(1,2*m, by=2),seq(2,2*m, by=2))
Upvotes: 2