Table of Contents
One can filter a vector in R by using the “subset” function or the “[]” brackets. The “subset” function allows for more complex filtering conditions, while the “[]” brackets are used for simpler filtering based on specific values or indices. Both methods involve specifying the vector to be filtered and the desired conditions or values to filter by. This allows for the creation of a new vector containing only the filtered elements.
Filter a Vector in R (4 Examples)
You can use the following methods to filter a vector in R:
Method 1: Filter for Elements Equal to Some Value
#filter for elements equal to 8
x[x == 8]
Method 2: Filter for Elements Based on One Condition
#filter for elements less than 8
x[x < 8]Method 3: Filter for Elements Based on Multiple Conditions
#filter for elements less than 8 or greater than 12
x[(x < 8) | (x > 12)]Method 4: Filter for Elements in List
#filter for elements equal to 2, 6, or 12 x[x %in% c(2, 6, 12)]
The following examples show how to use each method in practice.
Example 1: Filter for Elements Equal to Some Value
The following code shows how to filter a vector in R for elements that are equal to 8:
#create vector x <- c(1, 2, 2, 4, 6, 8, 8, 8, 12, 15) #filter for elements equal to 8 x[x == 8] [1] 8 8 8
We can just as easily filter for elements that are not equal to 8:
#create vector x <- c(1, 2, 2, 4, 6, 8, 8, 8, 12, 15) #filter for elements not equal to 8 x[x != 8] [1] 1 2 2 4 6 12 15
Example 2: Filter for Elements Based on One Condition
The following code shows how to filter a vector in R for elements that are less than 8:
#create vector x <- c(1, 2, 2, 4, 6, 8, 8, 8, 12, 15) #filter for elements less than 8 x[x < 8] [1] 1 2 2 4 6
Example 3: Filter for Elements Based on Multiple Conditions
The following code shows how to filter a vector in R for elements that are less than 8 or greater than 12:
#create vector x <- c(1, 2, 2, 4, 6, 8, 8, 8, 12, 15) #filter for elements less than 8 x[(x < 8) | (x > 12)] [1] 1 2 2 4 6 15
Example 4: Filter for Elements in List
The following code shows how to filter a vector in R for elements that are equal to values in a list:
#create vector x <- c(1, 2, 2, 4, 6, 8, 8, 8, 12, 15) #filter for elements equal to 2, 6, or 12 x[x %in% c(2, 6, 12)] [1] 2 2 6 12
Cite this article
stats writer (2024). How can one filter a vector in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-one-filter-a-vector-in-r/
stats writer. "How can one filter a vector in R?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-one-filter-a-vector-in-r/.
stats writer. "How can one filter a vector in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-one-filter-a-vector-in-r/.
stats writer (2024) 'How can one filter a vector in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-one-filter-a-vector-in-r/.
[1] stats writer, "How can one filter a vector in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can one filter a vector in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
