Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Levels | Factors
R Introduction: Part I
course content

Conteúdo do Curso

R Introduction: Part I

R Introduction: Part I

1. Basic Syntax and Operations
2. Basic Data Types and Vectors
3. Factors

bookLevels

Let's revisit the Levels label: you often see it when working with factor outputs. What if you want to view all possible values a factor can take?

To display all levels of a factor, which are the distinct categorical values it holds, use the levels() function with the factor variable as the argument. Let's take a look at an example:

12345
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output all the levels levels(curr_f)
copy

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:

12345678910
# 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'))
copy

Observing the difference is instructive. Try it out for yourself!

Tarefa
test

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':

  1. Convert the grades vector to a factor, capturing the required order, and store it in the grades_f variable.
  2. Display the entire grades_f variable.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 3
toggle bottom row

bookLevels

Let's revisit the Levels label: you often see it when working with factor outputs. What if you want to view all possible values a factor can take?

To display all levels of a factor, which are the distinct categorical values it holds, use the levels() function with the factor variable as the argument. Let's take a look at an example:

12345
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output all the levels levels(curr_f)
copy

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:

12345678910
# 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'))
copy

Observing the difference is instructive. Try it out for yourself!

Tarefa
test

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':

  1. Convert the grades vector to a factor, capturing the required order, and store it in the grades_f variable.
  2. Display the entire grades_f variable.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 3
toggle bottom row

bookLevels

Let's revisit the Levels label: you often see it when working with factor outputs. What if you want to view all possible values a factor can take?

To display all levels of a factor, which are the distinct categorical values it holds, use the levels() function with the factor variable as the argument. Let's take a look at an example:

12345
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output all the levels levels(curr_f)
copy

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:

12345678910
# 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'))
copy

Observing the difference is instructive. Try it out for yourself!

Tarefa
test

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':

  1. Convert the grades vector to a factor, capturing the required order, and store it in the grades_f variable.
  2. Display the entire grades_f variable.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Let's revisit the Levels label: you often see it when working with factor outputs. What if you want to view all possible values a factor can take?

To display all levels of a factor, which are the distinct categorical values it holds, use the levels() function with the factor variable as the argument. Let's take a look at an example:

12345
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output all the levels levels(curr_f)
copy

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:

12345678910
# 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'))
copy

Observing the difference is instructive. Try it out for yourself!

Tarefa
test

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':

  1. Convert the grades vector to a factor, capturing the required order, and store it in the grades_f variable.
  2. Display the entire grades_f variable.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 3. Capítulo 3
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt