← Back to R Course | Chapter 7: Data Structures | Lesson 6 of 7

which()

In this page:

  1. which()

which()

which(condition) returns the *indices* where a logical vector is TRUE, rather than the values themselves. This is useful when you need to know the position of matching elements, not just filter them out. which.max() and which.min() are shortcuts for finding the index of the largest/smallest value.

Note: which.max(v) is a shortcut for which(v == max(v))[1], finding the position of the largest value in one call.

Example: which()

markup
v <- c(3, 7, 2, 9, 4)

print(which(v > 4))
cat("Index of max value:", which.max(v), "\n")
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.