How To Delete Items in Vector?
We can delete an item or multiple items based on index from R Vector by passing the negated index as a vector in square brackets after the vector. The return value would be a new vector with the items already removed, but the original vector remains not changed.
The Syntax:
x[-indices] |
Examples:
m <- c(3, 4, 5, 6, 7)
cat("delete an item")
indices <- c(3)
result <- m[-indices]
print(result)
m <- c("p", "q", "r", "s")
cat("delete multiple items")
indices <- c(2, 3)
result <- m[-indices]
print(result)
#output:
delete an item
[1] 3 4 6 7
delete multiple items
[1] "p" "s"
Conclusion
Deleting items from a vector is done by passing the negated index as a vector in square brackets after the vector.
