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") |
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 |
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 |
which(vec == "X" | vec == "B") # Multiple logical conditions # 2 4 5 6