[R] Combining two data frames

Adrian Johnson oriolebaltimore at gmail.com
Fri Sep 19 22:58:59 CEST 2014


Hi:
Appreciate if I could get some help.

I have two data frames.

I want to combine these two dfs bases on first column.


df1 :

Subject    G1     G2    G3

A              10     1       0

B              20      2        20


df2:

Subject     m1    m2   m3

A              20      9     30

B                0      1    10


Resulting df :

Subject        G1    G2   G3     m1   m2  m3
A                  10      1    0       20     9    30
B                   20      2  20      0       1   10


Sometimes subjects in both df don't match.  since the data is large,
if I do cbind I am not sure about order and common subjects.



Thanks a lot for your help.


Following is dput of real data:


df1 <- structure(list(Subject = c("A-04-1332", "A-04-1336", "A-04-1337",
"A-04-1341", "A-04-1342", "A-04-1343", "A-04-1346",
"A-04-1347", "A-04-1348", "A-04-1349"), hsa.let.7a.1 = c(8788L,
2745L, 11447L, 8081L, 9291L, 44912L, 72521L, 2401L, 61251L, 134859L
), hsa.let.7a.2 = c(17806L, 5517L, 22864L, 16271L, 19194L, 89333L,
146114L, 4992L, 122469L, 270005L), hsa.let.7a.3 = c(8865L, 2743L,
11669L, 8235L, 9710L, 44633L, 73427L, 2540L, 61533L, 136403L),
    hsa.let.7b = c(72280L, 56297L, 70053L, 42974L, 56710L, 283102L,
    236254L, 5966L, 310874L, 596434L), hsa.let.7c = c(20743L,
    4848L, 10001L, 4070L, 10773L, 45697L, 60397L, 2313L, 35303L,
    51923L), hsa.let.7d = c(8187L, 12983L, 14028L, 14611L, 14971L,
    17719L, 7028L, 610L, 13165L, 20890L), hsa.let.7e = c(2745L,
    1068L, 4637L, 3747L, 4716L, 17202L, 25589L, 2147L, 8244L,
    21777L), hsa.let.7f.1 = c(1L, 0L, 5L, 3L, 5L, 11L, 5L, 0L,
    24L, 30L), hsa.let.7f.2 = c(404L, 176L, 1081L, 1130L, 1055L,
    3097L, 3998L, 119L, 5612L, 14844L)), .Names = c("Subject",
"hsa.let.7a.1", "hsa.let.7a.2", "hsa.let.7a.3", "hsa.let.7b",
"hsa.let.7c", "hsa.let.7d", "hsa.let.7e", "hsa.let.7f.1", "hsa.let.7f.2"
), row.names = c(NA, 10L), class = "data.frame")


df2 <- structure(list(Subject = c("A-04-1332", "A-04-1337", "A-04-1338",
"A-04-1341", "A-04-1343", "A-04-1347", "A-04-1348",
"A-04-1350", "A-04-1356", "A-04-1357"), A1BG = c(404L,
1387L, 436L, 2225L, 2444L, 838L, 618L, 1035L, 2812L, 309L), A1CF = c(0L,
1L, 3L, 1L, 2L, 0L, 0L, 0L, 1L, 0L), A2BP1 = c(454L, 7L, 18L,
117L, 9L, 1L, 3L, 3193L, 123L, 2L), A2LD1 = c(413L, 1100L, 334L,
882L, 990L, 697L, 2151L, 1102L, 1088L, 578L), A2M = c(44670L,
40872L, 8162L, 8183L, 29555L, 5252L, 46763L, 2099L, 11868L, 31205L
), A2ML1 = c(667L, 6L, 420L, 82L, 33L, 303L, 88L, 225L, 181L,
244L), A4GALT = c(2384L, 2821L, 491L, 1588L, 3685L, 151L, 702L,
528L, 2564L, 1005L), A4GNT = c(13L, 15L, 2L, 0L, 2L, 2L, 4L,
1L, 0L, 0L), AAA1 = c(0L, 2L, 0L, 0L, 2L, 0L, 0L, 4L, 2L, 0L)), .Names
= c("Subject",
"A1BG", "A1CF", "A2BP1", "A2LD1", "A2M", "A2ML1", "A4GALT", "A4GNT",
"AAA1"), row.names = c(NA, 10L), class = "data.frame")



Thanks



More information about the R-help mailing list