How can specific elements be removed from a vector in R?

How can specific elements be removed from a vector in R?

Removing specific elements from a vector in R can be achieved through the use of various functions such as the “which” and “!= (not equal)” operators. These functions allow for the identification and selection of specific elements within a vector based on certain criteria. Once the elements to be removed have been identified, they can be excluded from the original vector using the “subset” function or by reassigning the vector with the remaining elements. Additionally, the “na.omit” function can be used to remove any missing values from a vector. Overall, the process of removing specific elements from a vector in R involves systematically selecting and excluding elements based on their characteristics, resulting in a modified vector without the unwanted elements.

Remove Specific Elements from Vector in R


You can use the following basic syntax to remove specific elements from a vector in R:

#remove 'a', 'b', 'c' from my_vector
my_vector[! my_vector %in% c('a', 'b, 'c')]

The following examples show how to use this syntax in practice.

Example 1: Remove Elements from Character Vector 

The following code shows how to remove elements from a character vector in R:

#define vector
x <- c('Mavs', 'Nets', 'Hawks', 'Bucks', 'Spurs', 'Suns')

#remove 'Mavs' and 'Spurs' from vector
x <- x[! x %in% c('Mavs', 'Spurs')]

#view updated vector
x

[1] "Nets"  Hawks" "Bucks" "Suns" 

Notice that both ‘Mavs’ and ‘Spurs’ were removed from the vector.

Example 2: Remove Elements from Numeric Vector 

The following code shows how to remove elements from a numeric vector in R:

#define numeric vector
x <- c(1, 2, 2, 2, 3, 4, 5, 5, 7, 7, 8, 9, 12, 12, 13)

#remove 1, 4, and 5
x <- x[! x %in% c(1, 4, 5)]

#view updated vector
x

[1]  2  2  2  3  7  7  8  9 12 12 13

Notice that every occurrence of the values 1, 4, and 5 were removed from the vector.

We can also specify a range of values that we’d like to remove from the numeric vector:

#define numeric vector
x <- c(1, 2, 2, 2, 3, 4, 5, 5, 7, 7, 8, 9, 12, 12, 13)

#remove values between 2 and 10
x <- x[! x %in% 2:10]

#view updated vector
x

[1]  1 12 12 13

Notice that every value between 2 and 10 was removed from the vector.

We can also remove values greater or less than a specific number:

#define numeric vector
x <- c(1, 2, 2, 2, 3, 4, 5, 5, 7, 7, 8, 9, 12, 12, 13)

#remove values less than 3 or greater than 10
x <- x[!(x < 3 | x > 10)]

#view updated vector
x

[1] 3 4 5 5 7 7 8 9

Cite this article

stats writer (2024). How can specific elements be removed from a vector in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-specific-elements-be-removed-from-a-vector-in-r/

stats writer. "How can specific elements be removed from a vector in R?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/how-can-specific-elements-be-removed-from-a-vector-in-r/.

stats writer. "How can specific elements be removed from a vector in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-specific-elements-be-removed-from-a-vector-in-r/.

stats writer (2024) 'How can specific elements be removed from a vector in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-specific-elements-be-removed-from-a-vector-in-r/.

[1] stats writer, "How can specific elements be removed from a vector in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can specific elements be removed from a vector in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top