@@ -34,6 +34,12 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
3434 ans
3535 }
3636
37+ cast_with_atts = function (x , f ) {
38+ ans = f(x )
39+ if (! is.null(attributes(x ))) attributes(ans ) = attributes(x )
40+ ans
41+ }
42+
3743 if (nrow(i )) for (a in seq_along(icols )) {
3844 # - check that join columns have compatible types
3945 # - do type coercions if necessary on just the shallow local copies for the purpose of join
@@ -117,18 +123,15 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
117123 }
118124 if (coerce_x ) {
119125 if (verbose ) catf(" Coercing double column %s (which contains no fractions) to type integer to match type of %s.\n " , iname , xname )
120- val = as.integer(i [[ic ]])
121- if (! is.null(attributes(i [[ic ]]))) attributes(val ) = attributes(i [[ic ]]) # to retain Date for example; 3679
126+ val = cast_with_atts(i [[ic ]], as.integer ) # to retain Date for example; 3679
122127 set(i , j = ic , value = val )
123128 set(callersi , j = ic , value = val ) # change the shallow copy of i up in [.data.table to reflect in the result, too.
124129 if (length(ic_idx )> 1L ) {
125130 xc_idx = xcols [ic_idx ]
126131 for (b in which(vapply_1c(x [0L , ..xc_idx ], getClass ) == " double" )) {
127132 xb = xcols [b ]
128- val = as.integer(x [[xb ]])
129- if (! is.null(attributes(x [[xb ]]))) attributes(val ) = attributes(x [[xb ]])
130133 if (verbose ) catf(" Coercing double column %s (which contains no fractions) to type integer to match type of %s.\n " , paste0(" x." , names(x )[xb ]), xname )
131- set(x , j = xb , value = val )
134+ set(x , j = xb , value = cast_with_atts( x [[ xb ]], as.integer ) )
132135 }
133136 }
134137 }
@@ -139,17 +142,14 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
139142 }
140143 } else {
141144 if (verbose ) catf(" Coercing integer column %s to type double for join to match type of %s.\n " , iname , xname )
142- val = as.double(i [[ic ]])
143- if (! is.null(attributes(i [[ic ]]))) attributes(val ) = attributes(i [[ic ]]) # to retain Date for example; 3679
145+ val = cast_with_atts(i [[ic ]], as.double )
144146 set(i , j = ic , value = val )
145147 if (length(ic_idx )> 1L ) {
146148 xc_idx = xcols [ic_idx ]
147149 for (b in which(vapply_1c(x [0L , ..xc_idx ], getClass ) == " integer" )) {
148150 xb = xcols [b ]
149- val = as.double(x [[xb ]])
150- if (! is.null(attributes(x [[xb ]]))) attributes(val ) = attributes(x [[xb ]])
151151 if (verbose ) catf(" Coercing integer column %s to type double for join to match type of %s.\n " , paste0(" x." , names(x )[xb ]), xname )
152- set(x , j = xb , value = val )
152+ set(x , j = xb , value = cast_with_atts( x [[ xb ]], as.double ) )
153153 }
154154 }
155155 }
0 commit comments