Skip to content

Commit e952f3b

Browse files
committed
use floor/ceiling for first/last bin values when rounding
1 parent 935c55f commit e952f3b

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

R/bin_data.R

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,14 @@ bin_data <- function(x = NULL, binCol = NULL, bins = 10, binType = "explicit", b
149149
roundbins <- as.integer(roundbins)
150150
if(roundbins < 0) stop("roundbins should be non-negative")
151151
numbins <- length(LBs) # store the number of bins before rounding
152-
LBs <- round(LBs, roundbins)
153-
RBs <- round(RBs, roundbins)
152+
LBs <- c(
153+
floor(LBs[1L]) + floor((LBs[1L] - floor(LBs[1L])) * 10^roundbins)/10^roundbins,
154+
round(tail(LBs, -1), roundbins)
155+
)
156+
RBs <- c(
157+
round(head(RBs, -1), roundbins),
158+
floor(tail(RBs, 1)) + ceiling((tail(RBs, 1) - floor(tail(RBs, 1))) * 10^roundbins)/10^roundbins
159+
)
154160
if(length(unique(LBs)) != numbins){
155161
stop(paste0("roundbins = ", roundbins, " makes some bins indistinguishable. Try increasing this value"))
156162
}
@@ -196,13 +202,6 @@ bin_data <- function(x = NULL, binCol = NULL, bins = 10, binType = "explicit", b
196202
} else if(boundaryType == "(lorc"){
197203
binData[binDT, on = list(BinCol > LB, BinCol <= RB), Bin := Bin]
198204
}
199-
200-
# Check for unbinned data
201-
if(is.numeric(roundbins)){
202-
if(nrow(binData[!is.na(BinCol) & is.na(Bin)]) > 0){
203-
warning("Some values are missing bins because roundbins is too small. Increase roundbins to prevent this.")
204-
}
205-
}
206205

207206
#--------------------------------------------------
208207
# Return the desired result

0 commit comments

Comments
 (0)