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 Sincerely yours, Aleksander
One option is to put all the data frames into a list then use the Reduce function. On Mon, Dec 17, 2012 at 9:03 AM, Vasilchenko Aleksander < vasilchenko.a.p@gmail.com> 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 > > Sincerely yours, > Aleksander > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Gregory (Greg) L. Snow Ph.D. 538280@gmail.com [[alternative HTML version deleted]]
Hi, You could use either: Reduce(function(...) merge(...,by="date"),list(dat1,dat2,dat3,dat4)) #or library(reshape) merge_recurse(list(dat1,dat2,dat3,dat4), by="date") A.K. ? ----- Original Message ----- From: Vasilchenko Aleksander <vasilchenko.a.p at gmail.com> To: r-help at r-project.org Cc: Sent: Monday, December 17, 2012 11:03 AM Subject: [R] Merge more than 2 dataframe 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 Sincerely yours, Aleksander ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
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.4577647If 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