Data Science Desktop Survival Guide
by Graham Williams |
|||||
Ordered Factor |
20180723 Given our knowledge that compass directions have an obvious order, we convert the direction variables into an ordered factor. We do so using ordered=TRUE with base::factor().
# Note the names of the wind direction variables.
ds %>% select(contains("_dir")) %>% names() %T>% print() -> vnames
# Convert these variables from character to factor.
ds[vnames] %<>% lapply(factor, levels=compass, ordered=TRUE) %>% data.frame() %>% as_tibble() # Confirm they are now factors. ds[vnames] %>% sapply(class)
We can again obtain a distribution of the variables to confirm that all we have changed is the data type.
|
# Verify the distribution has not changed.
ds %>% select(contains("_dir")) %>% sapply(table)
|