[R] Merge more than 2 dataframe

David Winsemius dwinsemius at comcast.net
Mon Dec 17 22:06:30 CET 2012


On Dec 17, 2012, at 8:03 AM, Vasilchenko Aleksander wrote:

> Hello.
> I have for example 4 or more dataframe which like such this example:
>  date      value
> 2006-11 0.4577647
> 2006-12       NaN
> 2006-10 0.1577647
> 2006-11 0.3577647
> 2006-12       NaN
> 2007-01       NaN
> 2007-02       NaN
> 2007-03 0.2956429
> 2007-01 0.3677647
> 2007-02       NaN
> They have the same length. I need merge by date undefined number of
> dataframes. End result output will look like such example
>  date      name1          name2      name3         name4   ....
> 2006-11 0.4577647     0.4577647   0.4577647  NaN
> 2006-12       NaN        NaN           0.4577647  0.4577647
> 2006-10 0.1577647     0.1577647   0.4577647  NaN
> 2006-11 0.3577647     0.8577647   0.4577647  0.4577647
> 2006-12       NaN        0.1577647   0.4577647  0.4577647
> 2007-01       NaN        0.2277647   NaN           0.1777647
> 2007-02       NaN        0.4577647   0.4577647  0.4477647
> 2007-03 0.2956429     NaN            0.4577647  0.1577647
> 2007-01 0.3677647     0.4577647   NaN7         0.4577647
> 2007-02       NaN        0.6577647   0.4577647  0.4577647

If they all have the same number of rows and date values then it's just as easy to use cbind:

do.call(cbind, dat1, dat2[-1], dat3[-1], dat4[-1] )

-- 
David Winsemius
Alameda, CA, USA




More information about the R-help mailing list