which(): Find an index under condition.

The function which() is widely used to check which indices in a vector satisfy certain condition.

The function which.min() is a widely used function to find the index where maximum and minimum value exists in a vector, data.frame.

The basic usage of which() and which.min() function is written in the exercise.

Instruction

  • Put x > 4 in which function.
  • Find an index where a maximum value exists by using which.max function.
# Create a variable x x <- iris$Sepal.Length # Logical vector x>4 # Fix the following code to save indices where the value is larger than 4 into variable a. a <- which(x>4) # Access the values where x > 4. x[a] # Find an index where a minimum value exists. which.min(x) # Find an index where a maximum value exists. which.max(x)

Previous: 1-14 | max(), min(): Maxima and Minima

Next: 1-16 | Indexing Basics

Back to Main