To remove a specific element from a vector in R, you can use the indexing notation to subset the vector excluding the element to be removed.
In this example,
my_vector
with some elements.my_vector <- c(1, 2, 3, 4, 5)
value_to_remove <- 3
my_vector <- my_vector[my_vector != value_to_remove]
print(paste('Modified vector after removing', value_to_remove, ':', my_vector))
[1] "Modified vector after removing 3 : 1 2 4 5"
In this example,
my_vector
with some elements.my_vector <- c(1, 2, 3, 4, 5)
value_to_remove <- 6
my_vector <- my_vector[my_vector != value_to_remove]
print(paste('Original vector:', my_vector))
[1] "Original vector: 1 2 3 4 5"
In this tutorial, we learned How to Remove Specific Element from a List in R language with well detailed examples.