[英]Merging dataframes in R
I have two dataframes. 我有两个数据框。 One is like the following 一个像下面
> head(df1)
dropOffZip hour transition Day7
1 622 0 72 1
2 04745 0 1 1
3 05823 0 1 1
4 06490 0 1 2
5 06807 0 1 2
And the second one is like following : 第二个如下:
head(df2)
dropOffZip Day7 hour Median Count
1 622 1 0 60 1
2 622 2 8 60 1
3 622 3 8 60 1
4 622 7 12 60 1
Now I want to make df3
by merging df1
and df2
based on common value for dropOffZip
, Day7
and hour
. 现在我想通过基于dropOffZip
, Day7
和hour
通用值合并df1
和df2
来制作df3
。 The issue is while all the combination of day, hour, dropOffZip are available in df1, it's not the case for df2. 问题是df1中可以使用day,hour,dropOffZip的所有组合,而df2则不是。 So, in the merged df3, I still want to have rows for those combinations missing in df1, but the corresponding value for Median
and Count
should be assigned 0
. 因此,在合并的df3中,我仍然希望df1中缺少这些组合的行,但是应该将Median
和Count
的对应值分配为0
。 Could anyone suggest how to achieve this merging? 有人可以建议如何实现这种合并吗?
The final df3
should be like : 最终的df3
应该像:
>head(df3)
dropOffZip Day7 hour Median Count Transition
1 622 1 0 60 1 72
2 04745 1 0 0 0 1
Here the second row gives Median = 0
and Count = 0
because we don't have any column for dropOffZip
04745
in data frame df2
这里的第二行给出Median = 0
和Count = 0
因为在数据帧df2
没有dropOffZip
04745
任何列
Try giving all = TRUE in merge and remove the unwanted NA using complete.cases(df3). 尝试在合并中给出all = TRUE,并使用complete.cases(df3)删除不需要的NA。 Else add a new column called median and assign it to NA. 否则,添加一个称为中位数的新列,并将其分配给NA。 Just rbind it and remove the unwanted rows with NA using complete.cases. 只需rbind并使用complete.cases使用NA删除不需要的行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.