Skip to content

Commit

Permalink
retain NA in character when joining to factor (#3810)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdowle authored Sep 2, 2019
1 parent 4dd9548 commit 3d0dc92
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 5 additions & 3 deletions R/bmerge.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
stop("Attempting roll join on factor column when joining x.",names(x)[xc]," to i.",names(i)[ic],". Only integer, double or character columns may be roll joined.")
if (xclass=="factor" && iclass=="factor") {
if (verbose) cat("Matching i.",names(i)[ic]," factor levels to x.",names(x)[xc]," factor levels.\n",sep="")
set(i, j=ic, value=chmatch(levels(i[[ic]]), levels(x[[xc]]), nomatch=0L)[i[[ic]]])
set(i, j=ic, value=chmatch(levels(i[[ic]]), levels(x[[xc]]), nomatch=0L)[i[[ic]]]) # nomatch=0L otherwise a level that is missing would match to NA values
next
} else {
if (xclass=="character") {
Expand All @@ -57,11 +57,13 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
next
} else if (iclass=="character") {
if (verbose) cat("Matching character column i.",names(i)[ic]," to factor levels in x.",names(x)[xc],".\n",sep="")
set(i, j=ic, value=chmatch(i[[ic]], levels(x[[xc]]), nomatch=0L))
newvalue = chmatch(i[[ic]], levels(x[[xc]]), nomatch=0L)
if (anyNA(i[[ic]])) newvalue[is.na(i[[ic]])] = NA_integer_ # NA_character_ should match to NA in factor, #3809
set(i, j=ic, value=newvalue)
next
}
# else incompatible join type error below (factors can only join to factors or character)
}
stop("Incompatible join types: x.", names(x)[xc], " (",xclass,") and i.", names(i)[ic], " (",iclass,"). Factor columns must join to factor or character columns.")
}
if (xclass == iclass) {
if (verbose) cat("i.",names(i)[ic]," has same type (",xclass,") as x.",names(x)[xc],". No coercion needed.\n", sep="")
Expand Down
6 changes: 6 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -15864,6 +15864,12 @@ test(2095, any(grepl('override', capture.output(dcast(DT, a~b, fun.aggregate=len
# gmean intermediate can overflow integers without warning, #986
test(2096, data.table(a=c(1L,1L), v=c(2e9L, 2e9L))[, mean(v), a], data.table(a=1L, V1=2e9))

# NA_character joining to NA factor, #3809
DT = data.table(fac = as.factor(c("a",NA,"b")), v=1:3, key="fac")
test(2097.1, DT[.(NA_character_)], data.table(fac=NA_character_, v=2L, key="fac"))
test(2097.2, DT[J(NA)], error="Factor columns must join to factor or character columns")
test(2097.3, DT[J(NA_integer_)], error="Factor columns must join to factor or character columns")


###################################
# Add new tests above this line #
Expand Down

0 comments on commit 3d0dc92

Please sign in to comment.