Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Deleting Elements from Vectors | Basic Data Types and Vectors
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

bookDeleting Elements from Vectors

Since you've learned how to add elements to a vector, it's only natural to learn how to remove them as well. Everything builds up to this.

To remove an element from a vector, use the - sign before the index of the element you wish to remove. Remember, this action doesn't directly overwrite your vector, so reassignment is usually necessary. To remove multiple elements, you can provide a vector of indices after the - sign. For instance, to remove the third element from the grades vector, the operation would look like this:

12345
# Vector of grades and names grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Remove the third element grades[-3]
copy

Similarly, to eliminate the second and fourth elements from the initial grades vector, you would proceed as follows:

12345
# Vector of grades and names grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Remove the second and fourth elements grades[-c(2,4)]
copy
Task
test

Swipe to show code editor

From the previous task, you have the prices vector with the prices of 6 items. Your task is:

  1. Remove the 'Dining chair' item from the prices vector. This item is the fourth in the vector. Save the changes by reassignment.
  2. Display the modified prices vector.

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 2. Chapter 9
toggle bottom row

bookDeleting Elements from Vectors

Since you've learned how to add elements to a vector, it's only natural to learn how to remove them as well. Everything builds up to this.

To remove an element from a vector, use the - sign before the index of the element you wish to remove. Remember, this action doesn't directly overwrite your vector, so reassignment is usually necessary. To remove multiple elements, you can provide a vector of indices after the - sign. For instance, to remove the third element from the grades vector, the operation would look like this:

12345
# Vector of grades and names grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Remove the third element grades[-3]
copy

Similarly, to eliminate the second and fourth elements from the initial grades vector, you would proceed as follows:

12345
# Vector of grades and names grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Remove the second and fourth elements grades[-c(2,4)]
copy
Task
test

Swipe to show code editor

From the previous task, you have the prices vector with the prices of 6 items. Your task is:

  1. Remove the 'Dining chair' item from the prices vector. This item is the fourth in the vector. Save the changes by reassignment.
  2. Display the modified prices vector.

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 2. Chapter 9
toggle bottom row

bookDeleting Elements from Vectors

Since you've learned how to add elements to a vector, it's only natural to learn how to remove them as well. Everything builds up to this.

To remove an element from a vector, use the - sign before the index of the element you wish to remove. Remember, this action doesn't directly overwrite your vector, so reassignment is usually necessary. To remove multiple elements, you can provide a vector of indices after the - sign. For instance, to remove the third element from the grades vector, the operation would look like this:

12345
# Vector of grades and names grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Remove the third element grades[-3]
copy

Similarly, to eliminate the second and fourth elements from the initial grades vector, you would proceed as follows:

12345
# Vector of grades and names grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Remove the second and fourth elements grades[-c(2,4)]
copy
Task
test

Swipe to show code editor

From the previous task, you have the prices vector with the prices of 6 items. Your task is:

  1. Remove the 'Dining chair' item from the prices vector. This item is the fourth in the vector. Save the changes by reassignment.
  2. Display the modified prices vector.

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!

Since you've learned how to add elements to a vector, it's only natural to learn how to remove them as well. Everything builds up to this.

To remove an element from a vector, use the - sign before the index of the element you wish to remove. Remember, this action doesn't directly overwrite your vector, so reassignment is usually necessary. To remove multiple elements, you can provide a vector of indices after the - sign. For instance, to remove the third element from the grades vector, the operation would look like this:

12345
# Vector of grades and names grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Remove the third element grades[-3]
copy

Similarly, to eliminate the second and fourth elements from the initial grades vector, you would proceed as follows:

12345
# Vector of grades and names grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Remove the second and fourth elements grades[-c(2,4)]
copy
Task
test

Swipe to show code editor

From the previous task, you have the prices vector with the prices of 6 items. Your task is:

  1. Remove the 'Dining chair' item from the prices vector. This item is the fourth in the vector. Save the changes by reassignment.
  2. Display the modified prices vector.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 9
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