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

Example: Filtering Vector in R

x[x %in% c(1, 5, 7, 9)]        # Subsetting vector object
# [1] 1 5 7 9 5 1

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top