Deleting 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]
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)]
Swipe to start coding
From the previous task, you have the prices
vector with the prices of 6 items. Your task is:
- Remove the
'Dining chair'
item from theprices
vector. This item is the fourth in the vector. Save the changes by reassignment. - Display the modified
prices
vector.
Lösning
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Awesome!
Completion rate improved to 3.85
Deleting Elements from Vectors
Svep för att visa menyn
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]
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)]
Swipe to start coding
From the previous task, you have the prices
vector with the prices of 6 items. Your task is:
- Remove the
'Dining chair'
item from theprices
vector. This item is the fourth in the vector. Save the changes by reassignment. - Display the modified
prices
vector.
Lösning
Tack för dina kommentarer!
Awesome!
Completion rate improved to 3.85single