[BioC] Combine two data frames with different columns

Sean Davis sdavis2 at mail.nih.gov
Fri Feb 1 13:40:39 CET 2008


On Feb 1, 2008 7:28 AM, Daniel Brewer <daniel.brewer at icr.ac.uk> wrote:
> I would like to join two dataframes that have the majority of columns in
> common but some different.  Let me give you an example:
>
> Dataframe 1
>
> ID      COL1    COL2    COL3
> 1       a       a       a
> 2       b       b       b
> 3       c       c       c
>
> Dataframe 2
>
> ID      COL1    COL2    COL4
> 4       a       a       d
> 5       b       b       d
>
> And the joined dataframe I would like is:
>
> ID      COL1    COL2    COL3    COL4
> 1       a       a       a       NA
> 2       b       b       b       NA
> 3       c       c       c       NA
> 4       a       a       NA      d
> 5       b       b       NA      d
>
> Any ideas?

One solution is in the CRAN package reshape:

df1 <- data.frame(matrix(letters[1:16],nc=4))
df2 <- data.frame(matrix(letters[1:16],nc=4))
colnames(df1) <- 1:4
colnames(df2) <- 2:5
rbind.fill(df1,df2)

     1 2 3 4    5
1    a e i m <NA>
2    b f j n <NA>
3    c g k o <NA>
4    d h l p <NA>
5 <NA> a e i    m
6 <NA> b f j    n
7 <NA> c g k    o
8 <NA> d h l    p

There are certainly other answers, but I find the reshape package to
be a useful one anyway.

Sean



More information about the Bioconductor mailing list