合并具有不同行数的数据框
Merging Data Frames with Different Number of Rows
我有两个具有以下格式的数据框:
Date COL1 COL2 COL3 COL4
7/1/2015 Company1 0 0 0
7/2/2015 Company1 1 0 0
7/3/2015 Company1 0 0 8
7/4/2015 Company1 0 3 0
7/1/2015 Company2 0 0 0
7/2/2015 Company2 5 9 10
7/3/2015 Company2 0 0 0
7/4/2015 Company2 1 0 0
7/1/2015 Company3 0 0 0
第二个数据框如下所示:
Date COL1 COL2 COL3 COL4
7/1/2014 Company1 1 6 4
7/3/2014 Company1 4 9 5
7/4/2014 Company1 5 2 6
7/5/2014 Company1 4 2 8
7/2/2014 Company2 8 9 32
7/3/2014 Company2 9 12 8
7/1/2014 Company3 4 5 6
我想合并它们,最后看起来像这样:
Date COL1 COL2 COL3 COL4 Date COL1 COL2 COL3 COL4
7/1/2014 Company1 1 6 4 7/1/2015 Company1 0 0 0
7/2/2014 Company1 0 0 0 7/2/2015 Company1 1 0 0
7/3/2014 Company1 4 9 5 7/3/2015 Company1 0 0 8
7/4/2014 Company1 5 2 6 7/4/2015 Company1 0 3 0
7/5/2014 Company1 4 2 8 7/5/2015 Company1 0 0 0
7/1/2014 Company2 0 0 0 7/1/2015 Company2 0 0 0
7/2/2014 Company2 8 9 32 7/2/2015 Company2 5 9 10
7/3/2014 Company2 9 12 8 7/3/2015 Company2 0 0 0
7/4/2014 Company2 0 0 0 7/4/2015 Company2 1 0 0
7/1/2014 Company3 4 5 6 7/1/2015 Company3 0 0 0
所以基本上我想按日期和公司合并。目标是即使其中一个数据帧中缺少数据点,也要把日期排成一行,并将其替换为 0。
如果有人能提供帮助,将不胜感激!
您可以尝试这样的操作:
#parse dates
df1$Date <- as.Date(df1$Date,format="%m/%d/%Y")
df2$Date <- as.Date(df2$Date,format="%m/%d/%Y")
#create day/month column
df1$day_month <- format(df1$Date, "%m/%d")
df2$day_month <- format(df2$Date, "%m/%d")
#merge using day/month and company, all=T will print all rows
merge(df1,df2,by=c("day_month","COL1"),all=T)
我有两个具有以下格式的数据框:
Date COL1 COL2 COL3 COL4
7/1/2015 Company1 0 0 0
7/2/2015 Company1 1 0 0
7/3/2015 Company1 0 0 8
7/4/2015 Company1 0 3 0
7/1/2015 Company2 0 0 0
7/2/2015 Company2 5 9 10
7/3/2015 Company2 0 0 0
7/4/2015 Company2 1 0 0
7/1/2015 Company3 0 0 0
第二个数据框如下所示:
Date COL1 COL2 COL3 COL4
7/1/2014 Company1 1 6 4
7/3/2014 Company1 4 9 5
7/4/2014 Company1 5 2 6
7/5/2014 Company1 4 2 8
7/2/2014 Company2 8 9 32
7/3/2014 Company2 9 12 8
7/1/2014 Company3 4 5 6
我想合并它们,最后看起来像这样:
Date COL1 COL2 COL3 COL4 Date COL1 COL2 COL3 COL4
7/1/2014 Company1 1 6 4 7/1/2015 Company1 0 0 0
7/2/2014 Company1 0 0 0 7/2/2015 Company1 1 0 0
7/3/2014 Company1 4 9 5 7/3/2015 Company1 0 0 8
7/4/2014 Company1 5 2 6 7/4/2015 Company1 0 3 0
7/5/2014 Company1 4 2 8 7/5/2015 Company1 0 0 0
7/1/2014 Company2 0 0 0 7/1/2015 Company2 0 0 0
7/2/2014 Company2 8 9 32 7/2/2015 Company2 5 9 10
7/3/2014 Company2 9 12 8 7/3/2015 Company2 0 0 0
7/4/2014 Company2 0 0 0 7/4/2015 Company2 1 0 0
7/1/2014 Company3 4 5 6 7/1/2015 Company3 0 0 0
所以基本上我想按日期和公司合并。目标是即使其中一个数据帧中缺少数据点,也要把日期排成一行,并将其替换为 0。
如果有人能提供帮助,将不胜感激!
您可以尝试这样的操作:
#parse dates
df1$Date <- as.Date(df1$Date,format="%m/%d/%Y")
df2$Date <- as.Date(df2$Date,format="%m/%d/%Y")
#create day/month column
df1$day_month <- format(df1$Date, "%m/%d")
df2$day_month <- format(df2$Date, "%m/%d")
#merge using day/month and company, all=T will print all rows
merge(df1,df2,by=c("day_month","COL1"),all=T)