What will be an easy way to inter splice columns in R?

r

#1

Hello,

I am having two data frames and I want to inter splice them as follows (alternately taking a column from the first data frame, then from the second):

DF1:

Topic 1a 1b 1c
a 933.9 884.3 474.5
b 993.3 557.4 484.2
c 822.4 575.5 848.2
DF2:

Topic 2a 2b 2c
a 933.9 884.3 474.5
b 993.3 557.4 484.2
c 822.4 575.5 848.2
and I want to produce:

Topic 1a 2a 1b 2b 1c 2c
a 933.9 933.9 884.3 884.3 474.5 474.5
b 993.3 993.3 557.4 557.4 484.2 484.2
c 822.4 822.4 575.5 575.5 848.2 848.2

What would be an easy and short way to do such an operation?

Thank you.


#2

hello @adityashrm21,

x <- data.frame(a=1:3,b=3:5,c=7:9,key = 'a') 
 y <- data.frame(a=1:3,b=c('a','b','c'),c=c('x','y','z'),key = 'a')

Merge:

d <- cbind(x[1],y[1],x[2],y[2],x[3],y[3])

Referring to the columns by their position in the respective datasets by using [] will give the desired result.
Hope it helps!!