# NOT RUN {
# Generate data for the example
heartfailure2 <- heartfailure
heartfailure2[sample(seq(NROW(heartfailure2)), 20), "platelets"] <- NA
# Binning the carat variable. default type argument is "quantile"
bin <- binning(heartfailure2$platelets)
# Print bins class object
bin
# Summarise bins class object
summary(bin)
# Plot bins class object
plot(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
# bin <- binning(heartfailure2$platelets, nbins = 5, type = "kmeans")
# bin
# bin <- binning(heartfailure2$platelets, nbins = 5, type = "bclust")
# bin
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()) %>%
arrange(desc(freq)) %>%
head(10)
# Compare binned frequency by death_event using Viz
heartfailure2 %>%
mutate(platelets_bin = binning(heartfailure2$platelets) %>%
extract()) %>%
target_by(death_event) %>%
relate(platelets_bin) %>%
plot()
# }
Run the code above in your browser using DataLab