Dplyr inner_join with NA on character columns

I have two equal data frames

a <- c(1,2,3) b <- c(3,2,1) c <- c('a','b',NA) df1 <- data.frame(a=a, b=b, c=c, stringsAsFactors=FALSE) df2 <- data.frame(a=a, b=b, c=c, stringsAsFactors=FALSE) 

I would like to use dplyr::inner_join for

"return all rows from x, where y contains the corresponding values, and all columns from x and y" dplyr documentation

(this is all as they are equal), but it does not work with NA in column c (type chr ). Is this standard behavior not attaching to NA s?

for example

 library(dplyr) > inner_join(df1, df2) Joining by: c("a", "b", "c") abc 1 1 3 a 2 2 2 b 

does not join NA . However, I would like it to come back just like merge

 > merge(df1, df2) abc 1 1 3 a 2 2 2 b 3 3 1 <NA> 

Did I not understand how inner_join works in this case, and this behavior, as described?

Additional Information

inner_join matches NA in a numeric column

 a <- c(1,2,3) b <- c(3,2,NA) c <- c('a','b','c') df1 <- data.frame(a=a, b=b, c=c, stringsAsFactors=FALSE) df2 <- data.frame(a=a, b=b, c=c, stringsAsFactors=FALSE) > inner_join(df1, df2) Joining by: c("a", "b", "c") abc 1 1 3 a 2 2 2 b 3 3 NA c 

Edit

As @thelatemail points out, inner_join also works like merge when NA is in the factor column

 df1 <- data.frame(a=a, b=b, c=c, stringsAsFactors=T) df2 <- data.frame(a=a, b=b, c=c, stringsAsFactors=T) inner_join(df1, df2) Joining by: c("a", "b", "c") abc 1 1 3 a 2 2 2 b 3 3 3 <NA> 

Edit 2

Thanks to @shadow for pointing out this is a well-known question here and here

+7
inner-join r dplyr
source share
1 answer

This issue arose in version 0.4.1. This is now fixed in version 0.4.2:

 sessionInfo() ... other attached packages: [1] dplyr_0.4.2 ... > inner_join(df1, df2) Joining by: c("a", "b", "c") abc 1 1 3 a 2 2 2 b 3 3 1 <NA> 

Check merge:

 > merge(df1, df2) abc 1 1 3 a 2 2 2 b 3 3 1 <NA> > all.equal(inner_join(df1, df2), merge(df1, df2)) Joining by: c("a", "b", "c") [1] TRUE 
0
source share

All Articles