R programming: Merge tables
1
3
Entering edit mode
9.6 years ago
MAPK ★ 2.1k

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.8k views
ADD COMMENT
2
Entering edit mode
9.6 years ago
Brice Sarver ★ 3.8k
#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.'

ADD COMMENT
0
Entering edit mode

Sorry hold[list] generates error.

ADD REPLY
2
Entering edit mode

Missing a comma.

result.table <- hold[, list]
ADD REPLY
0
Entering edit mode

Sorry, typed too quickly. This is it.

ADD REPLY

Login before adding your answer.

Traffic: 1307 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6