Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Extracting List Elements | Lists
R Introduction: Part II
course content

Contenido del Curso

R Introduction: Part II

R Introduction: Part II

1. Matrices
2. Data Frames
3. Lists

bookExtracting List Elements

Good. Now let's find out how to extract certain elements of a list. The indexing of lists is the same as the indexing of vectors, i.e., the first element has index 1, the second has index 2, and so on. For example, let's extract the second element of the test list from the previous chapter.

1234
# Creating a list test <- list(text = "Text", number = 42, logical = TRUE) # Extracting the second element test[2]
copy

As you might notice, we extracted the element of list as list itself! It follows that for the example above, you can not perform any arithmetical operations with the extracted element since it is a list, not a number. To output an element from the list with its type, place its index inside double square brackets. For the example above, this will look like this:

123
test <- list(text = "Text", number = 42, logical = TRUE) # Extract the second element as numerical test[[2]]
copy

If the list has namings, you can use the dollar $ sign to access the value with its type straightaway. Also, you can pass the element's name to access its value within both single or double square brackets (like, test$text, test['text'], or test[['text']]).

Tarea
test

Swipe to show code editor

Given list info from the previous chapter. You need to complete the following tasks:

  1. Extract the first element as a list.
  2. Output class of the element from task 1 using class() function.
  3. Extract the fourth element as a numeric type.
  4. Output class of the element from task 3 using class() function.

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 3. Capítulo 3
toggle bottom row

bookExtracting List Elements

Good. Now let's find out how to extract certain elements of a list. The indexing of lists is the same as the indexing of vectors, i.e., the first element has index 1, the second has index 2, and so on. For example, let's extract the second element of the test list from the previous chapter.

1234
# Creating a list test <- list(text = "Text", number = 42, logical = TRUE) # Extracting the second element test[2]
copy

As you might notice, we extracted the element of list as list itself! It follows that for the example above, you can not perform any arithmetical operations with the extracted element since it is a list, not a number. To output an element from the list with its type, place its index inside double square brackets. For the example above, this will look like this:

123
test <- list(text = "Text", number = 42, logical = TRUE) # Extract the second element as numerical test[[2]]
copy

If the list has namings, you can use the dollar $ sign to access the value with its type straightaway. Also, you can pass the element's name to access its value within both single or double square brackets (like, test$text, test['text'], or test[['text']]).

Tarea
test

Swipe to show code editor

Given list info from the previous chapter. You need to complete the following tasks:

  1. Extract the first element as a list.
  2. Output class of the element from task 1 using class() function.
  3. Extract the fourth element as a numeric type.
  4. Output class of the element from task 3 using class() function.

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 3. Capítulo 3
toggle bottom row

bookExtracting List Elements

Good. Now let's find out how to extract certain elements of a list. The indexing of lists is the same as the indexing of vectors, i.e., the first element has index 1, the second has index 2, and so on. For example, let's extract the second element of the test list from the previous chapter.

1234
# Creating a list test <- list(text = "Text", number = 42, logical = TRUE) # Extracting the second element test[2]
copy

As you might notice, we extracted the element of list as list itself! It follows that for the example above, you can not perform any arithmetical operations with the extracted element since it is a list, not a number. To output an element from the list with its type, place its index inside double square brackets. For the example above, this will look like this:

123
test <- list(text = "Text", number = 42, logical = TRUE) # Extract the second element as numerical test[[2]]
copy

If the list has namings, you can use the dollar $ sign to access the value with its type straightaway. Also, you can pass the element's name to access its value within both single or double square brackets (like, test$text, test['text'], or test[['text']]).

Tarea
test

Swipe to show code editor

Given list info from the previous chapter. You need to complete the following tasks:

  1. Extract the first element as a list.
  2. Output class of the element from task 1 using class() function.
  3. Extract the fourth element as a numeric type.
  4. Output class of the element from task 3 using class() function.

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!

Good. Now let's find out how to extract certain elements of a list. The indexing of lists is the same as the indexing of vectors, i.e., the first element has index 1, the second has index 2, and so on. For example, let's extract the second element of the test list from the previous chapter.

1234
# Creating a list test <- list(text = "Text", number = 42, logical = TRUE) # Extracting the second element test[2]
copy

As you might notice, we extracted the element of list as list itself! It follows that for the example above, you can not perform any arithmetical operations with the extracted element since it is a list, not a number. To output an element from the list with its type, place its index inside double square brackets. For the example above, this will look like this:

123
test <- list(text = "Text", number = 42, logical = TRUE) # Extract the second element as numerical test[[2]]
copy

If the list has namings, you can use the dollar $ sign to access the value with its type straightaway. Also, you can pass the element's name to access its value within both single or double square brackets (like, test$text, test['text'], or test[['text']]).

Tarea
test

Swipe to show code editor

Given list info from the previous chapter. You need to complete the following tasks:

  1. Extract the first element as a list.
  2. Output class of the element from task 1 using class() function.
  3. Extract the fourth element as a numeric type.
  4. Output class of the element from task 3 using class() function.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 3. Capítulo 3
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt