# NOT RUN {
data(efc)
# non-grouped
table(efc$neg_c_7)
# split into 3 groups
table(split_var(efc$neg_c_7, n = 3))
# split multiple variables into 3 groups
split_var(efc, neg_c_7, pos_v_4, e17age, n = 3, append = FALSE)
frq(split_var(efc, neg_c_7, pos_v_4, e17age, n = 3, append = FALSE))
# original
table(efc$e42dep)
# two groups, non-inclusive cut-point
# vector split leads to unequal group sizes
table(split_var(efc$e42dep, n = 2))
# two groups, inclusive cut-point
# group sizes are equal
table(split_var(efc$e42dep, n = 2, inclusive = TRUE))
# Unlike dplyr's ntile(), split_var() never splits a value
# into two different categories, i.e. you always get a clean
# separation of original categories
library(dplyr)
x <- dplyr::ntile(efc$neg_c_7, n = 3)
table(efc$neg_c_7, x)
x <- split_var(efc$neg_c_7, n = 3)
table(efc$neg_c_7, x)
# works also with gouped data frames
mtcars %>%
split_var(disp, n = 3, append = FALSE) %>%
table()
mtcars %>%
group_by(cyl) %>%
split_var(disp, n = 3, append = FALSE) %>%
table()
# }
Run the code above in your browser using DataLab