I want to combine two tables A and B in R
A has col1 col2
B has colI colII
with
cbind()
you will get
col1 col2 colI colII
I want result to be like this:
col1 colI col2 colII
which will be much helpful in comparing.
I want to combine two tables A and B in R
A has col1 col2
B has colI colII
with
cbind()
you will get
col1 col2 colI colII
I want result to be like this:
col1 colI col2 colII
which will be much helpful in comparing.
Its not possible using cbind function. You will have to create your own logic for same.
# create table 1
t1 <- table(letters[1:2], sample(letters[1:2]))
# create table 2
t2 <- table(letters[3:4], sample(letters[3:4]))
df1 <- data.frame(cbind(t1,t2))
df1[,names(df1)][c("a", "c", "b", "d")]
# Output as below
# a c b d
#a 1 1 0 0
#b 0 0 1 1
For just two columns in each dataframe this approach seems the most straightforward:
# create 2 dataframes with 2 columns each:
df1 <- data.frame(col1 = c(1, 2, 3), col2 = c(4, 5, 6))
df2 <- data.frame(coli = c(10, 20, 30), colii = c(40, 50, 60))
# bind them together in your order:
df3 <- data.frame(df1$col1, df2$coli, df1$col2, df2$colii)