This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R programming: Merge tables

Hi Guys,

Is there a way to reorder the column of two tables, table1 and table2, according to the column names in the list (in the same order) and merge them into one table result.table.

list<-c("MAPK", "BCL2", "ALB", "MAPKK", "MEKK", "JUNK", "STR", "BRCA1")

table1

MAPK   MAPKK   MEKK   JUNK   STR
5      5       6      7      9
3      4       5      7      8

table2

BCL2   ALB   BRCA1
4      4     3
4      4     3

result.table

MAPK   BCL2   ALB   MAPKK   MEKK   JUNK   STR   BRCA1
5      4      4     5       6      7      9     3
3      4      4     4       5      7      8     3

Thank you!

r

1 answer

#combine the tables
hold <- cbind(table1, table2)

#order by 'list'
result.table <- hold[ , list]

This assumes that table1 and table2 are data frames with column names as indicated in 'list.'

Sorry hold[list] generates error.

Missing a comma.

result.table <- hold[, list]

Sorry, typed too quickly. This is it.

Log in to answer this question.