我有两个数据帧具有以下格式:
function gobackDir(count) {
var back_count = count;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","http://neha:8080/myServer/goback",true);
xmlhttp.send();
xmlhttp.onreadystatechange=function($event) {
if ($event.currentTarget.readyState==4 && $event.currentTarget.status==200) {
back_count--;
if (back_count > 0) {
gobackDir(back_count);
}
else {
var server_response = JSON.parse($event.currentTarget.response);
set_currentDirectory(server_response);
set_directoryContent(server_response);
}
}
}
}
第二个数据框如下所示:
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和Company合并。目标是即使从其中一个数据帧中丢失数据点,也要将日期排成一行,并将其替换为0' s。
如果有人能提供帮助,我们将不胜感激!
答案 0 :(得分:2)
您可以尝试这样的事情:
#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)