Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Indexing in Factors | Section
Practice
Projects
Quizzes & Challenges
Cuestionarios
Challenges
/
Essential R Programming for Absolute Beginners - 1768563985826

bookIndexing in Factors

Indexing in factors works the same way as with vectors: you select elements by their position or by a sequence of positions.

Basic Indexing

You can retrieve specific values from a factor by placing their indices inside square brackets.

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

The result shows the selected values, but R still lists all levels of the factor beneath the output.

Dropping Unused Levels

If you want to display only the levels that appear in the result, use the drop = T (T or TRUE) parameter.

12345
curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Show only levels that appear in the result curr_f[c(3, 5), drop = T]
copy

Sequences

When selecting a range of elements, the colon operator (:) generates consecutive indices. For example, 5:10 creates the sequence 5, 6, 7, 8, 9, 10. This makes it easier to extract multiple elements at once.

Tarea

Swipe to start coding

You have a dataset of blood types in a factor format. Your task is to:

  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.

Solución

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 22
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

close

bookIndexing in Factors

Desliza para mostrar el menú

Indexing in factors works the same way as with vectors: you select elements by their position or by a sequence of positions.

Basic Indexing

You can retrieve specific values from a factor by placing their indices inside square brackets.

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

The result shows the selected values, but R still lists all levels of the factor beneath the output.

Dropping Unused Levels

If you want to display only the levels that appear in the result, use the drop = T (T or TRUE) parameter.

12345
curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Show only levels that appear in the result curr_f[c(3, 5), drop = T]
copy

Sequences

When selecting a range of elements, the colon operator (:) generates consecutive indices. For example, 5:10 creates the sequence 5, 6, 7, 8, 9, 10. This makes it easier to extract multiple elements at once.

Tarea

Swipe to start coding

You have a dataset of blood types in a factor format. Your task is to:

  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.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 22
single

single

some-alt