Reputation: 107
I have different data.frames
of 1 row that I would like to join to form one big data.frame
. The thing is that I want my colnames
to go from jan 2002
to dic 2011
, and fill these columns with my data frames
even if for one column there is no value. Examples on data.frames (There can be with "discrete" dates):
df1:
ene 2002 ene 2002 feb 2002 feb 2002 mar 2002 mar 2002
69 MA 38 MA 38 MA
df2:
ago 2004 ago 2004 sep 2004 sep 2004 oct 2004 oct 2004
114 MB 102 MB 49 M
df3:
oct 2011 oct 2011 nov 2011 nov 2011 dic 2011 dic 2011
10 A 9 A 20 MA
df4("Discrete data.frame"):
jan 2008 jan 2008 jul 2009 jul 2009 dic 2010 dic 2010
20 MA 200 B 100 MB
Data.frame wanted (desire Output):
ene 2002 ene 2002 feb 2002 feb 2002 mar 2002 mar 2002...ago 2004 ago 2004 sep 2004 sep 2004 oct 2004 oct 2004...jan 2008 jan 2008...jul 2009 jul 2009...dic 2010 dic 2010... oct 2011 oct 2011 nov 2011 nov 2011 dic 2011 dic 2011
69 MA 38 MA 38 MA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
NA NA NA NA NA NA 114 MB 102 MB 49 M NA NA NA NA NA NA NA NA NA NA NA NA
NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 10 A 9 A 20 MA
NA NA NA NA NA NA NA NA NA NA NA NA 20 MA 200 B 100 MB NA NA NA NA NA NA
In this example all other columns with no value would be NA
: For example ene 2003 feb 2003 ..... nov 2005 dic 2005 ...... all month in year 2006 or 2007 ( Just saying random month that were not in the example but I want them to appear in my output)
Upvotes: 0
Views: 134
Reputation: 118799
Use plyr
's rbind.fill
rbind.fill(list(df1,df2,df3,df4))
Note: duplicate column names (ex: feb 2002) will be changed to feb 2002.1
.
Edit: This is what I get:
# ene2002 ene2002.1 feb2002 feb2002.1 mar2002 mar2002.1 ago2004 ago2004.1 sep2004
# 1 69 MA 38 MA 38 MA NA <NA> NA
# 2 NA <NA> NA <NA> NA <NA> 114 MB 102
# 3 NA <NA> NA <NA> NA <NA> NA <NA> NA
# 4 NA <NA> NA <NA> NA <NA> NA <NA> NA
# sep2004.1 oct2004 oct2004.1 oct2011 oct2011.1 nov2011 nov2011.1 dic2011 dic2011.1
# 1 <NA> NA <NA> NA <NA> NA <NA> NA <NA>
# 2 MB 49 M NA <NA> NA <NA> NA <NA>
# 3 <NA> NA <NA> 10 A 9 A 20 MA
# 4 <NA> NA <NA> NA <NA> NA <NA> NA <NA>
# jan2008 jan2008.1 jul2009 jul2009.1 dic2010 dic2010.1
# 1 NA <NA> NA <NA> NA <NA>
# 2 NA <NA> NA <NA> NA <NA>
# 3 NA <NA> NA <NA> NA <NA>
# 4 20 MA 200 B 100 MB
Upvotes: 1