You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# - do type coercions if necessary on just the shallow local copies for the purpose of join
40
-
# - handle factor columns appropriately
41
-
# Note that if i is keyed, if this coerces i's key gets dropped by set()
42
-
ic=icols[a]
43
-
xc=xcols[a]
44
-
x_merge_type= getClass(x[[xc]])
45
-
i_merge_type= getClass(i[[ic]])
46
-
xname= paste0("x.", names(x)[xc])
47
-
iname= paste0("i.", names(i)[ic])
48
-
if (!x_merge_type %chin% supported) stopf("%s is type %s which is not supported by data.table join", xname, x_merge_type)
49
-
if (!i_merge_type %chin% supported) stopf("%s is type %s which is not supported by data.table join", iname, i_merge_type)
50
-
if (x_merge_type=="factor"||i_merge_type=="factor") {
51
-
if (roll!=0.0&&a==length(icols))
52
-
stopf("Attempting roll join on factor column when joining %s to %s. Only integer, double or character columns may be roll joined.", xname, iname)
53
-
if (x_merge_type=="factor"&&i_merge_type=="factor") {
54
-
if (verbose) catf("Matching %s factor levels to %s factor levels.\n", iname, xname)
55
-
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
56
-
next
57
-
} else {
58
-
if (x_merge_type=="character") {
59
-
if (verbose) catf("Coercing factor column %s to type character to match type of %s.\n", iname, xname)
60
-
set(i, j=ic, value=val<-as.character(i[[ic]]))
61
-
set(callersi, j=ic, value=val) # factor in i joining to character in x will return character and not keep x's factor; e.g. for antaresRead #3581
62
-
next
63
-
} elseif (i_merge_type=="character") {
64
-
if (verbose) catf("Matching character column %s to factor levels in %s.\n", iname, xname)
if (anyNA(i[[ic]])) newvalue[is.na(i[[ic]])] =NA_integer_# NA_character_ should match to NA in factor, #3809
67
-
set(i, j=ic, value=newvalue)
37
+
if (nrow(i)) {
38
+
for (ain seq_along(icols)) {
39
+
# - check that join columns have compatible types
40
+
# - do type coercions if necessary on just the shallow local copies for the purpose of join
41
+
# - handle factor columns appropriately
42
+
# Note that if i is keyed, if this coerces i's key gets dropped by set()
43
+
ic=icols[a]
44
+
xc=xcols[a]
45
+
x_merge_type= getClass(x[[xc]])
46
+
i_merge_type= getClass(i[[ic]])
47
+
xname= paste0("x.", names(x)[xc])
48
+
iname= paste0("i.", names(i)[ic])
49
+
if (!x_merge_type %chin% supported) stopf("%s is type %s which is not supported by data.table join", xname, x_merge_type)
50
+
if (!i_merge_type %chin% supported) stopf("%s is type %s which is not supported by data.table join", iname, i_merge_type)
51
+
if (x_merge_type=="factor"||i_merge_type=="factor") {
52
+
if (roll!=0.0&&a==length(icols))
53
+
stopf("Attempting roll join on factor column when joining %s to %s. Only integer, double or character columns may be roll joined.", xname, iname)
54
+
if (x_merge_type=="factor"&&i_merge_type=="factor") {
55
+
if (verbose) catf("Matching %s factor levels to %s factor levels.\n", iname, xname)
56
+
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
68
57
next
58
+
} else {
59
+
if (x_merge_type=="character") {
60
+
if (verbose) catf("Coercing factor column %s to type character to match type of %s.\n", iname, xname)
61
+
set(i, j=ic, value=val<-as.character(i[[ic]]))
62
+
set(callersi, j=ic, value=val) # factor in i joining to character in x will return character and not keep x's factor; e.g. for antaresRead #3581
63
+
next
64
+
} elseif (i_merge_type=="character") {
65
+
if (verbose) catf("Matching character column %s to factor levels in %s.\n", iname, xname)
if (anyNA(i[[ic]])) newvalue[is.na(i[[ic]])] =NA_integer_# NA_character_ should match to NA in factor, #3809
68
+
set(i, j=ic, value=newvalue)
69
+
next
70
+
}
69
71
}
72
+
stopf("Incompatible join types: %s (%s) and %s (%s). Factor columns must join to factor or character columns.", xname, x_merge_type, iname, i_merge_type)
70
73
}
71
-
stopf("Incompatible join types: %s (%s) and %s (%s). Factor columns must join to factor or character columns.", xname, x_merge_type, iname, i_merge_type)
72
-
}
73
-
if (x_merge_type==i_merge_type) {
74
-
if (length(icols)>1&& is(x[[xc]], "Date") && is(i[[ic]], "Date")) {
75
-
set(x, j=xc, value=as.double(x[[xc]]))
76
-
set(i, j=ic, value=as.double(i[[ic]]))
77
-
if (verbose) catf("%s and %s are both Dates. R does not guarentee a type for Date internally, hence, coercing to double.\n", iname, xname)
78
-
} else {
74
+
if (x_merge_type==i_merge_type) {
79
75
if (verbose) catf("%s has same type (%s) as %s. No coercion needed.\n", iname, x_merge_type, xname)
80
-
}
81
-
next
82
-
}
83
-
if (x_merge_type=="character"||i_merge_type=="character"||
if (x_merge_type=="integer64"||i_merge_type=="integer64") {
99
-
nm= c(iname, xname)
100
-
if (x_merge_type=="integer64") { w=i; wc=ic; wclass=i_merge_type; } else { w=x; wc=xc; wclass=x_merge_type; nm=rev(nm) } # w is which to coerce
101
-
if (wclass=="integer"|| (wclass=="double"&&!isReallyReal(w[[wc]]))) {
102
-
if (verbose) catf("Coercing %s column %s%s to type integer64 to match type of %s.\n", wclass, nm[1L], if (wclass=="double") " (which contains no fractions)"else"", nm[2L])
103
-
set(w, j=wc, value=bit64::as.integer64(w[[wc]]))
104
-
} else stopf("Incompatible join types: %s is type integer64 but %s is type double and contains fractions", nm[2L], nm[1L])
105
-
} else {
106
-
# just integer and double left
107
-
if (i_merge_type=="double") {
108
-
if (!isReallyReal(i[[ic]])) {
109
-
# common case of ad hoc user-typed integers missing L postfix joining to correct integer keys
110
-
# we've always coerced to int and returned int, for convenience.
111
-
if (verbose) catf("Coercing double column %s (which contains no fractions) to type integer to match type of %s.\n", iname, xname)
112
-
val= as.integer(i[[ic]])
113
-
if (!is.null(attributes(i[[ic]]))) attributes(val) = attributes(i[[ic]]) # to retain Date for example; 3679
114
-
set(i, j=ic, value=val)
115
-
set(callersi, j=ic, value=val) # change the shallow copy of i up in [.data.table to reflect in the result, too.
93
+
if (x_merge_type=="integer64"||i_merge_type=="integer64") {
94
+
nm= c(iname, xname)
95
+
if (x_merge_type=="integer64") { w=i; wc=ic; wclass=i_merge_type; } else { w=x; wc=xc; wclass=x_merge_type; nm=rev(nm) } # w is which to coerce
96
+
if (wclass=="integer"|| (wclass=="double"&&!isReallyReal(w[[wc]]))) {
97
+
if (verbose) catf("Coercing %s column %s%s to type integer64 to match type of %s.\n", wclass, nm[1L], if (wclass=="double") " (which contains no fractions)"else"", nm[2L])
98
+
set(w, j=wc, value=bit64::as.integer64(w[[wc]]))
99
+
} else stopf("Incompatible join types: %s is type integer64 but %s is type double and contains fractions", nm[2L], nm[1L])
100
+
} else {
101
+
# just integer and double left
102
+
if (i_merge_type=="double") {
103
+
if (!isReallyReal(i[[ic]])) {
104
+
# common case of ad hoc user-typed integers missing L postfix joining to correct integer keys
105
+
# we've always coerced to int and returned int, for convenience.
106
+
if (verbose) catf("Coercing double column %s (which contains no fractions) to type integer to match type of %s.\n", iname, xname)
107
+
val= as.integer(i[[ic]])
108
+
if (!is.null(attributes(i[[ic]]))) attributes(val) = attributes(i[[ic]]) # to retain Date for example; 3679
109
+
set(i, j=ic, value=val)
110
+
set(callersi, j=ic, value=val) # change the shallow copy of i up in [.data.table to reflect in the result, too.
111
+
} else {
112
+
if (verbose) catf("Coercing integer column %s to type double to match type of %s which contains fractions.\n", xname, iname)
113
+
set(x, j=xc, value=as.double(x[[xc]]))
114
+
}
116
115
} else {
117
-
if (verbose) catf("Coercing integer column %s to type double to match type of %s which contains fractions.\n", xname, iname)
118
-
set(x, j=xc, value=as.double(x[[xc]]))
116
+
if (verbose) catf("Coercing integer column %s to type double for join to match type of %s.\n", iname, xname)
117
+
set(i, j=ic, value=as.double(i[[ic]]))
119
118
}
120
-
} else {
121
-
if (verbose) catf("Coercing integer column %s to type double for join to match type of %s.\n", iname, xname)
0 commit comments