Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Introduction to Lists in R | R Lists and Nested Data
Working with Data Structures in R

bookIntroduction to Lists in R

When you need to store different types of data together in R—such as numbers, text, and logical values—a list is the most flexible structure. Unlike vectors, which must contain elements of the same type, lists let you combine many types of objects, even other lists or data frames. Lists are especially useful when you want to keep related but dissimilar data in a single container, such as the results of a statistical analysis or the components of a model.

Note
Definition

In R, a list is a data structure that can contain elements of different types, including numbers, characters, logical values, and even other lists. This makes lists more versatile than vectors, which can only hold elements of a single type.

123
# Create a list with elements of different types my_list <- list(42, "hello", TRUE) print(my_list)
copy

In this example, you create a list called my_list that contains three elements: a numeric value (42), a character string ("hello"), and a logical value (TRUE). Each item keeps its original type, showing how lists can hold a mix of data types in one structure.

12345678
my_list <- list(42, "hello", TRUE) # Using double brackets to get the actual element second_element <- my_list[[2]] print(second_element) # Using single brackets to get a sublist sublist <- my_list[2] print(sublist)
copy

When you access elements in a list, you can use either double brackets ([[ ]]) or single brackets ([ ]). Double brackets extract the element itself, so my_list[[2]] gives you the character string "hello". Single brackets return a sublist containing the specified element, so my_list[2] gives you a list with one element—the second item. Use double brackets when you want the actual value, and single brackets when you want to keep the result as a list.

1. What is the main advantage of using a list over a vector in R?

2. Which syntax is used to access the second element of a list named my_list?

3. Can a list in R contain another list as an element?

question mark

What is the main advantage of using a list over a vector in R?

Select the correct answer

question mark

Which syntax is used to access the second element of a list named my_list?

Select the correct answer

question mark

Can a list in R contain another list as an element?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 1

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you explain more about how to add or remove elements from a list in R?

What happens if I try to access an element that doesn't exist in the list?

Can you show how to name the elements in a list and access them by name?

bookIntroduction to Lists in R

Deslize para mostrar o menu

When you need to store different types of data together in R—such as numbers, text, and logical values—a list is the most flexible structure. Unlike vectors, which must contain elements of the same type, lists let you combine many types of objects, even other lists or data frames. Lists are especially useful when you want to keep related but dissimilar data in a single container, such as the results of a statistical analysis or the components of a model.

Note
Definition

In R, a list is a data structure that can contain elements of different types, including numbers, characters, logical values, and even other lists. This makes lists more versatile than vectors, which can only hold elements of a single type.

123
# Create a list with elements of different types my_list <- list(42, "hello", TRUE) print(my_list)
copy

In this example, you create a list called my_list that contains three elements: a numeric value (42), a character string ("hello"), and a logical value (TRUE). Each item keeps its original type, showing how lists can hold a mix of data types in one structure.

12345678
my_list <- list(42, "hello", TRUE) # Using double brackets to get the actual element second_element <- my_list[[2]] print(second_element) # Using single brackets to get a sublist sublist <- my_list[2] print(sublist)
copy

When you access elements in a list, you can use either double brackets ([[ ]]) or single brackets ([ ]). Double brackets extract the element itself, so my_list[[2]] gives you the character string "hello". Single brackets return a sublist containing the specified element, so my_list[2] gives you a list with one element—the second item. Use double brackets when you want the actual value, and single brackets when you want to keep the result as a list.

1. What is the main advantage of using a list over a vector in R?

2. Which syntax is used to access the second element of a list named my_list?

3. Can a list in R contain another list as an element?

question mark

What is the main advantage of using a list over a vector in R?

Select the correct answer

question mark

Which syntax is used to access the second element of a list named my_list?

Select the correct answer

question mark

Can a list in R contain another list as an element?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 1
some-alt