How to Apply the which() Function in R (2 Examples)

In this post you’ll learn how to return index positions of certain values using the which function in the R programming language.

Introducing Example Data

vec <- c("A", "X", "C", "B", "B", "X")

Example 1: Return Index Positions of Certain Value in Vector Using which() Function

which(vec == "X")                 # Applying which function to vector
# 2 6

Example 2: Return Index Positions of Multiple Values in Vector Using which() Function

which(vec == "X" | vec == "B")    # Multiple logical conditions
# 2 4 5 6

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