# \donttest{
# Generate data for the example
heartfailure2 <- heartfailure
heartfailure2[sample(seq(NROW(heartfailure2)), 20), "platelets"] <- NA
# Binning the platelets variable. default type argument is "quantile"
bin <- binning(heartfailure2$platelets)
# Print bins class object
bin
# Using labels argument
bin <- binning(heartfailure2$platelets, nbins = 4,
labels = c("LQ1", "UQ1", "LQ3", "UQ3"))
bin
# Using another type argument
bin <- binning(heartfailure2$platelets, nbins = 5, type = "equal")
bin
bin <- binning(heartfailure2$platelets, nbins = 5, type = "pretty")
bin
# "kmeans" and "bclust" was implemented by classInt::classIntervals() function.
# So, you must install classInt package.
if (requireNamespace("classInt", quietly = TRUE)) {
bin <- binning(heartfailure2$platelets, nbins = 5, type = "kmeans")
bin
bin <- binning(heartfailure2$platelets, nbins = 5, type = "bclust")
bin
} else {
cat("If you want to use this feature, you need to install the 'classInt' package.\n")
}
x <- sample(1:1000, size = 50) * 12345679
bin <- binning(x)
bin
bin <- binning(x, approxy.lab = FALSE)
bin
# extract binned results
extract(bin)
# -------------------------
# Using pipes & dplyr
# -------------------------
library(dplyr)
# Compare binned frequency by death_event
heartfailure2 %>%
mutate(platelets_bin = binning(heartfailure2$platelets) %>%
extract()) %>%
group_by(death_event, platelets_bin) %>%
summarise(freq = n(), .groups = "drop") %>%
arrange(desc(freq)) %>%
head(10)
# }
Run the code above in your browser using DataLab