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

Course Content

R Introduction: Part II

R Introduction: Part II

1. Matrices
2. Data Frames
3. Lists

bookModifying

Now let's move on to list modification tools. First, let's consider methods of adding an element (or elements) to a list.

The first method is convenient for adding single values. You need to assign a new value to the new index or naming. For example, let's add a new element named integer with the value of 23L.

12345
# Creating a list test <- list(text = "Text", number = 42, logical = TRUE) # Add new value test['integer'] <- 23L test # Output modified list
copy

Note

Unlike in the list() function, you must use quotes for naming while adding a new element. You can also 'merge' two lists by placing them inside a vector. This approach will 'connect' two lists together.

123456
# Two lists list1 <- list("first", 10) list2 <- list("second", 20) # Merge two lists list12 <- c(list1, list2) list12 # Output its value
copy

Also, you can delete elements from the list. To do it, assign to the necessary index value NULL. For example, let's remove TRUE from the first example.

123456
test <- list(text = "Text", number = 42, logical = TRUE) test['integer'] <- 23L # Remove the third element test$logical <- NULL # Output modified list test
copy

As you can see, you removed the TRUE element. Finally, to change the existing value of the list, reassign the new value to the existing index/naming. For example, if you write test[1] <- "word", then instead of "Text", the first element will be "word".

Task
test

Swipe to show code editor

Given list info from the previous chapter. Your tasks are:

  1. Rewrite the fourth element to 44 (this chapter is the 44th).
  2. Remove the third element (which is 1).
  3. Add a new element named Level with the value of 'Beginner'.
  4. Output modified list info.

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 4
toggle bottom row

bookModifying

Now let's move on to list modification tools. First, let's consider methods of adding an element (or elements) to a list.

The first method is convenient for adding single values. You need to assign a new value to the new index or naming. For example, let's add a new element named integer with the value of 23L.

12345
# Creating a list test <- list(text = "Text", number = 42, logical = TRUE) # Add new value test['integer'] <- 23L test # Output modified list
copy

Note

Unlike in the list() function, you must use quotes for naming while adding a new element. You can also 'merge' two lists by placing them inside a vector. This approach will 'connect' two lists together.

123456
# Two lists list1 <- list("first", 10) list2 <- list("second", 20) # Merge two lists list12 <- c(list1, list2) list12 # Output its value
copy

Also, you can delete elements from the list. To do it, assign to the necessary index value NULL. For example, let's remove TRUE from the first example.

123456
test <- list(text = "Text", number = 42, logical = TRUE) test['integer'] <- 23L # Remove the third element test$logical <- NULL # Output modified list test
copy

As you can see, you removed the TRUE element. Finally, to change the existing value of the list, reassign the new value to the existing index/naming. For example, if you write test[1] <- "word", then instead of "Text", the first element will be "word".

Task
test

Swipe to show code editor

Given list info from the previous chapter. Your tasks are:

  1. Rewrite the fourth element to 44 (this chapter is the 44th).
  2. Remove the third element (which is 1).
  3. Add a new element named Level with the value of 'Beginner'.
  4. Output modified list info.

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 4
toggle bottom row

bookModifying

Now let's move on to list modification tools. First, let's consider methods of adding an element (or elements) to a list.

The first method is convenient for adding single values. You need to assign a new value to the new index or naming. For example, let's add a new element named integer with the value of 23L.

12345
# Creating a list test <- list(text = "Text", number = 42, logical = TRUE) # Add new value test['integer'] <- 23L test # Output modified list
copy

Note

Unlike in the list() function, you must use quotes for naming while adding a new element. You can also 'merge' two lists by placing them inside a vector. This approach will 'connect' two lists together.

123456
# Two lists list1 <- list("first", 10) list2 <- list("second", 20) # Merge two lists list12 <- c(list1, list2) list12 # Output its value
copy

Also, you can delete elements from the list. To do it, assign to the necessary index value NULL. For example, let's remove TRUE from the first example.

123456
test <- list(text = "Text", number = 42, logical = TRUE) test['integer'] <- 23L # Remove the third element test$logical <- NULL # Output modified list test
copy

As you can see, you removed the TRUE element. Finally, to change the existing value of the list, reassign the new value to the existing index/naming. For example, if you write test[1] <- "word", then instead of "Text", the first element will be "word".

Task
test

Swipe to show code editor

Given list info from the previous chapter. Your tasks are:

  1. Rewrite the fourth element to 44 (this chapter is the 44th).
  2. Remove the third element (which is 1).
  3. Add a new element named Level with the value of 'Beginner'.
  4. Output modified list info.

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!

Now let's move on to list modification tools. First, let's consider methods of adding an element (or elements) to a list.

The first method is convenient for adding single values. You need to assign a new value to the new index or naming. For example, let's add a new element named integer with the value of 23L.

12345
# Creating a list test <- list(text = "Text", number = 42, logical = TRUE) # Add new value test['integer'] <- 23L test # Output modified list
copy

Note

Unlike in the list() function, you must use quotes for naming while adding a new element. You can also 'merge' two lists by placing them inside a vector. This approach will 'connect' two lists together.

123456
# Two lists list1 <- list("first", 10) list2 <- list("second", 20) # Merge two lists list12 <- c(list1, list2) list12 # Output its value
copy

Also, you can delete elements from the list. To do it, assign to the necessary index value NULL. For example, let's remove TRUE from the first example.

123456
test <- list(text = "Text", number = 42, logical = TRUE) test['integer'] <- 23L # Remove the third element test$logical <- NULL # Output modified list test
copy

As you can see, you removed the TRUE element. Finally, to change the existing value of the list, reassign the new value to the existing index/naming. For example, if you write test[1] <- "word", then instead of "Text", the first element will be "word".

Task
test

Swipe to show code editor

Given list info from the previous chapter. Your tasks are:

  1. Rewrite the fourth element to 44 (this chapter is the 44th).
  2. Remove the third element (which is 1).
  3. Add a new element named Level with the value of 'Beginner'.
  4. Output modified list info.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt