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

Course Content

R Introduction: Part I

R Introduction: Part I

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

bookIndexing in Factors

Excellent! Now, let's delve into how to manipulate or modify an existing factor variable.

To begin, we can specify which values to display by using indexing. This operates in the same way as indexing a vector. For instance, to show the third and fifth entries of the curr_f factor, we can use the following indexing:

12345
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output the third and the fifth values curr_f[c(3,5)]
copy

When R presents the output, it lists all levels beneath, even though only a couple of categorical values were shown. This feature becomes particularly handy when dealing with extensive data and trying to remember all the unique values is impractical.

If it's not necessary to display every level, you can include the drop = TRUE (or drop = T) parameter within the brackets []. This will show only the levels retrieved from indexing. The same operation as mentioned earlier would be written like this:

12345
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output the third and the fifth values curr_f[c(3,5), drop = T]
copy

Using individual indices may not be practical when you need to extract a series of elements. In R, you can generate a vector of consecutive integers by separating the starting and ending numbers with a colon :. For instance, 5:10 will produce a sequence 5, 6, 7, 8, 9, 10. Apply this technique in the upcoming task.

Task

Considering the same dataset of blood types in a factor format, here are your tasks:

  1. Display the 3rd, 10th, and 15th elements of blood_gr, making sure to drop any unused levels (using the drop parameter).
  2. Show every element from the 15th to the 21st, inclusive.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
toggle bottom row

bookIndexing in Factors

Excellent! Now, let's delve into how to manipulate or modify an existing factor variable.

To begin, we can specify which values to display by using indexing. This operates in the same way as indexing a vector. For instance, to show the third and fifth entries of the curr_f factor, we can use the following indexing:

12345
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output the third and the fifth values curr_f[c(3,5)]
copy

When R presents the output, it lists all levels beneath, even though only a couple of categorical values were shown. This feature becomes particularly handy when dealing with extensive data and trying to remember all the unique values is impractical.

If it's not necessary to display every level, you can include the drop = TRUE (or drop = T) parameter within the brackets []. This will show only the levels retrieved from indexing. The same operation as mentioned earlier would be written like this:

12345
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output the third and the fifth values curr_f[c(3,5), drop = T]
copy

Using individual indices may not be practical when you need to extract a series of elements. In R, you can generate a vector of consecutive integers by separating the starting and ending numbers with a colon :. For instance, 5:10 will produce a sequence 5, 6, 7, 8, 9, 10. Apply this technique in the upcoming task.

Task

Considering the same dataset of blood types in a factor format, here are your tasks:

  1. Display the 3rd, 10th, and 15th elements of blood_gr, making sure to drop any unused levels (using the drop parameter).
  2. Show every element from the 15th to the 21st, inclusive.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
toggle bottom row

bookIndexing in Factors

Excellent! Now, let's delve into how to manipulate or modify an existing factor variable.

To begin, we can specify which values to display by using indexing. This operates in the same way as indexing a vector. For instance, to show the third and fifth entries of the curr_f factor, we can use the following indexing:

12345
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output the third and the fifth values curr_f[c(3,5)]
copy

When R presents the output, it lists all levels beneath, even though only a couple of categorical values were shown. This feature becomes particularly handy when dealing with extensive data and trying to remember all the unique values is impractical.

If it's not necessary to display every level, you can include the drop = TRUE (or drop = T) parameter within the brackets []. This will show only the levels retrieved from indexing. The same operation as mentioned earlier would be written like this:

12345
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output the third and the fifth values curr_f[c(3,5), drop = T]
copy

Using individual indices may not be practical when you need to extract a series of elements. In R, you can generate a vector of consecutive integers by separating the starting and ending numbers with a colon :. For instance, 5:10 will produce a sequence 5, 6, 7, 8, 9, 10. Apply this technique in the upcoming task.

Task

Considering the same dataset of blood types in a factor format, here are your tasks:

  1. Display the 3rd, 10th, and 15th elements of blood_gr, making sure to drop any unused levels (using the drop parameter).
  2. Show every element from the 15th to the 21st, inclusive.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Excellent! Now, let's delve into how to manipulate or modify an existing factor variable.

To begin, we can specify which values to display by using indexing. This operates in the same way as indexing a vector. For instance, to show the third and fifth entries of the curr_f factor, we can use the following indexing:

12345
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output the third and the fifth values curr_f[c(3,5)]
copy

When R presents the output, it lists all levels beneath, even though only a couple of categorical values were shown. This feature becomes particularly handy when dealing with extensive data and trying to remember all the unique values is impractical.

If it's not necessary to display every level, you can include the drop = TRUE (or drop = T) parameter within the brackets []. This will show only the levels retrieved from indexing. The same operation as mentioned earlier would be written like this:

12345
# Vector of currencies as factor curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Output the third and the fifth values curr_f[c(3,5), drop = T]
copy

Using individual indices may not be practical when you need to extract a series of elements. In R, you can generate a vector of consecutive integers by separating the starting and ending numbers with a colon :. For instance, 5:10 will produce a sequence 5, 6, 7, 8, 9, 10. Apply this technique in the upcoming task.

Task

Considering the same dataset of blood types in a factor format, here are your tasks:

  1. Display the 3rd, 10th, and 15th elements of blood_gr, making sure to drop any unused levels (using the drop parameter).
  2. Show every element from the 15th to the 21st, inclusive.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt