Skip to content

Commit 1bb60bf

Browse files
committed
indent loop
1 parent a75fb0e commit 1bb60bf

File tree

1 file changed

+73
-77
lines changed

1 file changed

+73
-77
lines changed

R/bmerge.R

Lines changed: 73 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -34,92 +34,88 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
3434
ans
3535
}
3636

37-
if (nrow(i)) for (a in seq_along(icols)) {
38-
# - check that join columns have compatible types
39-
# - 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-
} else if (i_merge_type=="character") {
64-
if (verbose) catf("Matching character column %s to factor levels in %s.\n", iname, xname)
65-
newvalue = chmatch(i[[ic]], levels(x[[xc]]), nomatch=0L)
66-
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 (a in 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
6857
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+
} else if (i_merge_type=="character") {
65+
if (verbose) catf("Matching character column %s to factor levels in %s.\n", iname, xname)
66+
newvalue = chmatch(i[[ic]], levels(x[[xc]]), nomatch=0L)
67+
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+
}
6971
}
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)
7073
}
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) {
7975
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" ||
84-
x_merge_type=="logical" || i_merge_type=="logical" ||
85-
x_merge_type=="factor" || i_merge_type=="factor") {
86-
if (anyNA(i[[ic]]) && allNA(i[[ic]])) {
87-
if (verbose) catf("Coercing all-NA %s (%s) to type %s to match type of %s.\n", iname, i_merge_type, x_merge_type, xname)
88-
set(i, j=ic, value=match.fun(paste0("as.", x_merge_type))(i[[ic]]))
8976
next
9077
}
91-
else if (anyNA(x[[xc]]) && allNA(x[[xc]])) {
92-
if (verbose) catf("Coercing all-NA %s (%s) to type %s to match type of %s.\n", xname, x_merge_type, i_merge_type, iname)
93-
set(x, j=xc, value=match.fun(paste0("as.", i_merge_type))(x[[xc]]))
94-
next
78+
if (x_merge_type=="character" || i_merge_type=="character" ||
79+
x_merge_type=="logical" || i_merge_type=="logical" ||
80+
x_merge_type=="factor" || i_merge_type=="factor") {
81+
if (anyNA(i[[ic]]) && allNA(i[[ic]])) {
82+
if (verbose) catf("Coercing all-NA %s (%s) to type %s to match type of %s.\n", iname, i_merge_type, x_merge_type, xname)
83+
set(i, j=ic, value=match.fun(paste0("as.", x_merge_type))(i[[ic]]))
84+
next
85+
}
86+
else if (anyNA(x[[xc]]) && allNA(x[[xc]])) {
87+
if (verbose) catf("Coercing all-NA %s (%s) to type %s to match type of %s.\n", xname, x_merge_type, i_merge_type, iname)
88+
set(x, j=xc, value=match.fun(paste0("as.", i_merge_type))(x[[xc]]))
89+
next
90+
}
91+
stopf("Incompatible join types: %s (%s) and %s (%s)", xname, x_merge_type, iname, i_merge_type)
9592
}
96-
stopf("Incompatible join types: %s (%s) and %s (%s)", xname, x_merge_type, iname, i_merge_type)
97-
}
98-
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+
}
116115
} 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]]))
119118
}
120-
} else {
121-
if (verbose) catf("Coercing integer column %s to type double for join to match type of %s.\n", iname, xname)
122-
set(i, j=ic, value=as.double(i[[ic]]))
123119
}
124120
}
125121
}

0 commit comments

Comments
 (0)