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
. The issue is while all the combination of day, hour, dropOffZip are available in df1, it's not the case for 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
. Could anyone suggest how to achieve this merging?
The final df3
should be like :
>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
Try giving all = TRUE in merge and remove the unwanted NA using complete.cases(df3). Else add a new column called median and assign it to NA. Just rbind it and remove the unwanted rows with NA using complete.cases.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.