How to Subset a Vector Object in R (Example Code)
In this tutorial you’ll learn how to subset the elements of a vector object in R.
Construction of Example Data
x <- c(1:10, 5:1) # Example vector in R x # Show example vector in RStudio console # [1] 1 2 3 4 5 6 7 8 9 10 5 4 3 2 1 |
x <- c(1:10, 5:1) # Example vector in R x # Show example vector in RStudio console # [1] 1 2 3 4 5 6 7 8 9 10 5 4 3 2 1
Example: Filtering Vector in R
x[x %in% c(1, 5, 7, 9)] # Subsetting vector object # [1] 1 5 7 9 5 1 |
x[x %in% c(1, 5, 7, 9)] # Subsetting vector object # [1] 1 5 7 9 5 1