speed_vector
should be converted to an ordinal factor since its categories have a natural ordering. By default, the function factor()
transforms speed_vector
into an unordered factor. To create an ordered factor, you have to add two additional arguments: ordered
and levels
.
factor(some_vector,
ordered = TRUE,
levels = c("lev1", "lev2" ...))
By setting the argument ordered
to TRUE
in the function factor()
, you indicate that the factor is ordered. With the argument levels
you give the values of the factor in the correct order.
From speed_vector
, create an ordered factor vector: factor_speed_vector
. Set ordered
to TRUE
, and set levels
to c("slow", "medium", "fast")
.
# Create speed_vector
speed_vector <- c("medium", "slow", "slow", "medium", "fast")
# Convert speed_vector to ordered factor vector
factor_speed_vector <-
# Print factor_speed_vector
factor_speed_vector
summary(factor_speed_vector)
# Create speed_vector
speed_vector <- c("medium", "slow", "slow", "medium", "fast")
# Add your code below
factor_speed_vector <- factor(speed_vector, ordered = TRUE, levels = c("slow", "medium", "fast"))
# Print factor_speed_vector
factor_speed_vector
summary(factor_speed_vector)
msg = "Do not change anything about the command that specifies the `speed_vector` variable."
test_object("speed_vector", undefined_msg = msg, incorrect_msg = msg)
test_function("factor", args = c("x", "ordered", "levels"),
incorrect_msg = c("The first argument you pass to `factor()` should be `speed_vector`.",
"Make sure to set `ordered = TRUE` inside your call of `factor()`.",
"Make sure to set `levels = c(\"slow\", \"medium\", \"fast\")` inside your call of `factor()`."))
test_object("factor_speed_vector", eq_condition = "equal",
incorrect_msg = "There's still something wrong with `factor_speed_vector`; make sure to only pass `speed_vector`, `ordered = TRUE` and `levels = c(\"slow\", \"medium\", \"fast\")` inside your call of `factor()`.")
success_msg("Great! Have a look at the console. It is now indicated that the Levels indeed have an order associated, with the `<` sign. Continue to the next exercise.")
Use the function factor()
to create factor_speed_vector
based on speed_character_vector
. The argument ordered
should be set to TRUE
since there is a natural ordering. Also, set levels = c("slow", "medium", "fast")
.