The “which” Function in R

  • Package: Base R (no specific package required)

  • Purpose: Returns the indices of the elements that are TRUE in a logical vector.

  • General Class: Data Indexing

  • Required Argument(s):

    • x: A logical vector.

  • Notable Optional Arguments:

    • arr.ind: A logical indicating whether to return array indices when x is an array.

  • Example:

  • # Example data for using the which function
    ages <- c(25, 30, 22, 28, 35)

    # Find the indices of elements greater than 30
    indices <- which(ages > 30)

    # Display the result
    print(indices)

  • In this example, the which function is used to find the indices of elements in the ages vector that are greater than 30. The resulting indices vector contains the positions where the condition is TRUE. The which function is commonly used for indexing and selecting elements based on logical conditions.

Previous
Previous

The “rep” Function in R

Next
Next

The “ifelse” Function in R