Зміст курсу
Вступ до R: Частина 1
Вступ до R: Частина 1
Рівні
Чудова робота! Можливо, ви пам'ятаєте рядок Levels
(рівні), який з'явяється під час виведення деяких факторів. Що робити, якщо вас цікавлять усі можливі значення ваших факторів?
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output all the levels levels(curr_f)
Interestingly, you can rearrange these levels without altering the actual data. Nonetheless, we sometimes encounter ordered factor variables. Take height, for instance: one might be classified as tall, medium, or short. This ordering implies tall > medium > short. R accommodates this by allowing you to specify the ordered
parameter as TRUE
. This organizes the variables alphabetically for textual values, or numerically for values that are numbers.
While numerical ordering is typically straightforward and desired, alphabetical ordering might not be appropriate. To establish a specific order, you also need to set the labels
parameter to a vector that lists your values in ascending order. Let's look at an example for clarity.
# Factors with no ordering factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Factors with ordering without labels parameter factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR'), ordered = T) # Factors with ordering with labels parameter factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR'), ordered = T, labels = c('USD', 'EUR', 'CHF', 'AUD', 'NOK'))
Observing the difference is instructive. Try it out for yourself!
Swipe to show code editor
Let's say you have a vector of grades ranging from 'A' to 'F'. You're tasked with converting this into an ordered factor with the sequence 'F < D < C < B < A':
- Convert the
grades
vector to a factor, capturing the required order, and store it in thegrades_f
variable. - Display the entire
grades_f
variable.
Дякуємо за ваш відгук!
Рівні
Чудова робота! Можливо, ви пам'ятаєте рядок Levels
(рівні), який з'явяється під час виведення деяких факторів. Що робити, якщо вас цікавлять усі можливі значення ваших факторів?
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output all the levels levels(curr_f)
Interestingly, you can rearrange these levels without altering the actual data. Nonetheless, we sometimes encounter ordered factor variables. Take height, for instance: one might be classified as tall, medium, or short. This ordering implies tall > medium > short. R accommodates this by allowing you to specify the ordered
parameter as TRUE
. This organizes the variables alphabetically for textual values, or numerically for values that are numbers.
While numerical ordering is typically straightforward and desired, alphabetical ordering might not be appropriate. To establish a specific order, you also need to set the labels
parameter to a vector that lists your values in ascending order. Let's look at an example for clarity.
# Factors with no ordering factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Factors with ordering without labels parameter factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR'), ordered = T) # Factors with ordering with labels parameter factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR'), ordered = T, labels = c('USD', 'EUR', 'CHF', 'AUD', 'NOK'))
Observing the difference is instructive. Try it out for yourself!
Swipe to show code editor
Let's say you have a vector of grades ranging from 'A' to 'F'. You're tasked with converting this into an ordered factor with the sequence 'F < D < C < B < A':
- Convert the
grades
vector to a factor, capturing the required order, and store it in thegrades_f
variable. - Display the entire
grades_f
variable.
Дякуємо за ваш відгук!
Рівні
Чудова робота! Можливо, ви пам'ятаєте рядок Levels
(рівні), який з'явяється під час виведення деяких факторів. Що робити, якщо вас цікавлять усі можливі значення ваших факторів?
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output all the levels levels(curr_f)
Interestingly, you can rearrange these levels without altering the actual data. Nonetheless, we sometimes encounter ordered factor variables. Take height, for instance: one might be classified as tall, medium, or short. This ordering implies tall > medium > short. R accommodates this by allowing you to specify the ordered
parameter as TRUE
. This organizes the variables alphabetically for textual values, or numerically for values that are numbers.
While numerical ordering is typically straightforward and desired, alphabetical ordering might not be appropriate. To establish a specific order, you also need to set the labels
parameter to a vector that lists your values in ascending order. Let's look at an example for clarity.
# Factors with no ordering factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Factors with ordering without labels parameter factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR'), ordered = T) # Factors with ordering with labels parameter factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR'), ordered = T, labels = c('USD', 'EUR', 'CHF', 'AUD', 'NOK'))
Observing the difference is instructive. Try it out for yourself!
Swipe to show code editor
Let's say you have a vector of grades ranging from 'A' to 'F'. You're tasked with converting this into an ordered factor with the sequence 'F < D < C < B < A':
- Convert the
grades
vector to a factor, capturing the required order, and store it in thegrades_f
variable. - Display the entire
grades_f
variable.
Дякуємо за ваш відгук!
Чудова робота! Можливо, ви пам'ятаєте рядок Levels
(рівні), який з'явяється під час виведення деяких факторів. Що робити, якщо вас цікавлять усі можливі значення ваших факторів?
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output all the levels levels(curr_f)
Interestingly, you can rearrange these levels without altering the actual data. Nonetheless, we sometimes encounter ordered factor variables. Take height, for instance: one might be classified as tall, medium, or short. This ordering implies tall > medium > short. R accommodates this by allowing you to specify the ordered
parameter as TRUE
. This organizes the variables alphabetically for textual values, or numerically for values that are numbers.
While numerical ordering is typically straightforward and desired, alphabetical ordering might not be appropriate. To establish a specific order, you also need to set the labels
parameter to a vector that lists your values in ascending order. Let's look at an example for clarity.
# Factors with no ordering factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Factors with ordering without labels parameter factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR'), ordered = T) # Factors with ordering with labels parameter factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR'), ordered = T, labels = c('USD', 'EUR', 'CHF', 'AUD', 'NOK'))
Observing the difference is instructive. Try it out for yourself!
Swipe to show code editor
Let's say you have a vector of grades ranging from 'A' to 'F'. You're tasked with converting this into an ordered factor with the sequence 'F < D < C < B < A':
- Convert the
grades
vector to a factor, capturing the required order, and store it in thegrades_f
variable. - Display the entire
grades_f
variable.