1

I have tree dataframes:

a:
        tag1    adj.P.Val1 DE1 
1     A1BG-AS1   0.9869732  0 
2        A2LD1   0.9924706  0 
3       A4GALT   0.9538785  0 

b:
        tag2    adj.P.Val2 DE2 
1         A1BG   0.9734810  0 
2     A1BG-AS1   0.7790235  0 
3        A2LD1   0.4763385  0 

c:
        tag3    adj.P.Val3 DE3 
1     A1BG-AS1   0.9742065  0 
2        A2LD1   0.2733337  0  
3         AAAS   0.2015485  0  

I want to merge these dataframe and it is important for me that the rows with same "tag" value placed in the same row in final dataframe:

    tag1    adj.P.Val1 DE1    tag2    adj.P.Val2 DE2  tag3   adj.P.Val3   DE3
1    NA          NA    NA     A1BG      0.9734810  0   NA         NA       NA
2  1BG-AS1   0.9869732  0     A1BG-AS1  0.7790235  0  A1BG-AS1   0.9742065  0
3    A2LD1   0.9924706  0     A2LD1     0.4763385  0   A2LD1     0.2733337  0
4   A4GALT   0.9538785  0      NA          NA      NA   NA        NA       NA
5    NA          NA    NA      NA          NA      NA  AAAS      0.2015485  0

What should I do?

Jaap
  • 81,064
  • 34
  • 182
  • 193
YBC
  • 39
  • 2
  • 8
  • 1
    You just need to use `merge` with `a` and `b` and merge the resulting dataframe with `c`. There's no reason to repeat the tags if they're going to be the same. Check the `merge` documentation with `?merge` – Bernardo Oct 09 '15 at 18:55
  • You can also check [here](http://stackoverflow.com/questions/33030756/merge-4-data-objects-with-different-columns-variables-in-r/33031393#33031393) – akrun Oct 09 '15 at 18:56

1 Answers1

2

This should give you want you want:

# rename the id-columns & wrap put the datafrmes in a list
lst <- lapply(list(a,b,c), function(x) {names(x)[1] <- "tag";x})
# merge the three separate dataframes into one
df <- Reduce(function(...) merge(..., all = TRUE, by = "tag"), lst)

this results in:

> df
       tag adj.P.Val1 DE1 adj.P.Val2 DE2 adj.P.Val3 DE3
1 A1BG-AS1  0.9869732   0  0.7790235   0  0.9742065   0
2    A2LD1  0.9924706   0  0.4763385   0  0.2733337   0
3   A4GALT  0.9538785   0         NA  NA         NA  NA
4     A1BG         NA  NA  0.9734810   0         NA  NA
5     AAAS         NA  NA         NA  NA  0.2015485   0
Jaap
  • 81,064
  • 34
  • 182
  • 193