OneBite.Dev - Coding blog in a bite size

remove a specific element from an array in R

Code snippet on how to remove a specific element from an array in R

  array <- c("apple", "banana", "kiwi")
  array <- array[array != "banana"]

This code creates an array with three elements, “apple”, “banana”, and “kiwi”. It then uses a conditional statement to “filter out” the element called “banana”. That is, it rewrites the array without the element “banana”. This is done by re-assigning the array variable (using the line array <- array[array != "banana"]) to the result of the conditional statement. The statement itself reads as: “for an array, keep all entries where the entry is not equal to string “banana”.

r