Description Usage Arguments Details Examples
Specify factor level orders
1 | level_factors(data, category_type, category, factor_data)
|
data |
data.frame with columns to be turned into factors and ordered |
category_type |
column name (unquoted) in data giving the name of
the overall category group to be joined on the corresponding
|
category |
column name (unquoted) to be turned into a factor |
factor_data |
data.frame with 3 columns: |
Use a data.frame to order the levels of factors in another. This means you can make a csv with all your factor orders specified and not worry about having factor orders all over your code.
Returns data untouched if the values in category_type
and category
in factor_data
don't align with the values in the data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | # Make the sub_group column into a factor, ordered in reverse alphabetical
# order
data <- data.frame(
sub_group = sample(letters[10:15], 20, replace = TRUE),
values = rnorm(20)
)
factor_data <- data.frame(
category_type = "A",
category = c(letters[10:15]),
order = 6:1
)
arrange(data, sub_group)
new_data <- level_factors(data, "A", sub_group, factor_data)
arrange(new_data, sub_group)
# You can store the orders of many factors in factor_data and apply
# to different columnns
factor_data <- data.frame(
category_type = rep(c("A", "B", "C"), times = c(6, 2, 4)),
category = c(letters[10:15], "B 1", "B 2", LETTERS[23:26]),
order = c(6:1, 1:2, c(2, 1, 4, 3))
)
data <- data.frame(
A_group = sample(letters[10:15], 20, replace = TRUE),
B_group = sample(c("B 1", "B 2"), 20, replace = TRUE),
C_group = sample(LETTERS[23:26], 20, replace = TRUE),
values = rnorm(20)
)
new_data <- data %>%
level_factors("A", A_group, factor_data) %>%
level_factors("B", B_group, factor_data) %>%
level_factors("C", C_group, factor_data)
levels(new_data$A_group)
levels(new_data$B_group)
levels(new_data$C_group)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.