I've been using the following function to create even bin variables:
## Even Bins Funtion
evenbins <- function(x, bin.count = 5, order = T) {
bin.size <- rep(length(x) %/% bin.count, bin.count)
bin.size <- bin.size + ifelse(1:bin.count <= length(x) %% bin.count, 1,0)
bin <- rep(1:bin.count, bin.size)
if(order) {
bin <- bin[rank(x, ties.method = "random")]
}
return(factor(bin, levels = 1:bin.count, ordered = order))
}
This works great to bin the numeric values, however, it groups NA's into the final group (in this case the 5th bin). So it does something like this if pivoted:
I'd like to tweak the function to remove the NA's from initial binning funciton and keep them as NA values so when I group the bin column it yields this:
Thanks in advance for reading and any help!!
SAMPLE CODE to work w/:
##set up fake dataset
df1 <- data.frame(x = c(1:450))
df2 <- data.frame(x = 1:50)
df2$x <- NA
df3 <- rbind (df1, df2 )
## Even Bins Funtion
evenbins <- function(x, bin.count = 5, order = T) {
bin.size <- rep(length(x) %/% bin.count, bin.count)
bin.size <- bin.size + ifelse(1:bin.count <= length(x) %% bin.count, 1,0)
bin <- rep(1:bin.count, bin.size)
if(order) {
bin <- bin[rank(x, ties.method = "random")]
}
return(factor(bin, levels = 1:bin.count, ordered = order))
}
df3$Bin <- evenbins(df3$x)
df3$isNA <- ifelse(is.na(df3$x) == TRUE, "# NA","complete")
t1 <- cbind(
table(df3$Bin)
,table(df3$Bin, df3$isNA)
)


Here's a simple modification - count the
NAs, remove them, and then tack them on again at the end: