which Function in R (5 Examples)

On this page, Iâ€™ll illustrate how to return index positions of certain values using the which function in R.

Letâ€™s do this:

Definition & Basic R Syntax of which Function

Definition: The which R function returns the indices of certain values based on a logical condition.

Basic R Syntax: Please find the basic R programming syntax of the which function below.

`which(my_data == 5)                                # Basic R syntax of which function`

In the following, Iâ€™ll show five examples for the application of the which function in R.

Example 1: Basic Application of which Function

Example 1 illustrates how to use the which() function in the R programming language. First, we have to create some example data:

```x <- c(1, 5, 4, 8, 4)                              # Create example vector
x                                                  # Print example vector
# 1 5 4 8 4```

You can see based on the previous output of the RStudio console that our example data is a vector containing five numeric values.

Letâ€™s assume that we want to get the index positions of the value 4. Then, we can apply the which function to our vector as shown below:

```which(x == 4)                                      # Apply which function to vector
# 3 5```

The which function returns the values 3 and 5, i.e. the third and the fifth element of our example vector contains the value 4.

Example 2: Applying which Function with Multiple Logical Conditions

The following R syntax explains how to use which() with more than one logical condition. Have a look at the following R syntax:

```which(x == 4 | x == 1)                             # which function with multiple conditions
# 1 3 5```

The previous R code returned each vector index position of elements that are either equal to 4 or equal to 1. We were able to return this result by using the |-operator.

Example 3: Count Occurrences Using which & length Functions

In this Example, Iâ€™ll show how to use the which function in combination with the length function to count the number of occurrences of certain values in R. For this, we simply need to wrap the length function around our previously used R code:

```length(which(x == 4 | x == 1))                     # Counting occurrences
# 3```

The RStudio console returns 3, i.e. the values 4 and 1 appear three times in our example vector.

Example 4: Subsetting Data Frame Rows Using which Function

The which command can also be used to subset data tables in R. In this Example, Iâ€™ll explain how to extract or remove certain data frame rows using the which function. First, we have to create some example data:

```data <- data.frame(x1 = 1:5,                       # Create example data
x2 = letters[1:5],
x3 = 3)
data                                               # Print example data
#   x1 x2 x3
# 1  1  a  3
# 2  2  b  3
# 3  3  c  3
# 4  4  d  3
# 5  5  e  3```

Our data matrix contains five rows and three columns. Now, we can apply the which function to select and retain only specific rows of our data:

```data[which(data\$x1 >= 3), ]                        # Subset of example data rows
#   x1 x2 x3
# 3  3  c  3
# 4  4  d  3
# 5  5  e  3```

As you can see, we have extracted only rows were the variable x1 has a value larger than or equal to 3.

Example 5: Subsetting Data Frame Columns Using which & colnames Functions

We can also use the which function to extract certain columns of our data frame. In this case, we are using the colnames function and the %in%-operator to create our logical object:

```data[ , which(colnames(data) %in% c("x1", "x3"))]  # Subset of example data columns
#   x1 x3
# 1  1  3
# 2  2  3
# 3  3  3
# 4  4  3
# 5  5  3```

The previous R code returned a new data frame containing of the variables x1 and x3 (i.e. x2 was removed).

Video & Further Resources

Do you need more explanations on the content of this tutorial? Then you may have a look at the following video of my YouTube channel. In the video, Iâ€™m explaining the R codes of this article in RStudio.

Furthermore, you might want to have a look at the related articles on this homepage. I have published numerous other articles already.

In this R programming tutorial you learned how to give the TRUE indices of a logical object. Please let me know in the comments section below, if you have any additional questions.

Subscribe to the Statistics Globe Newsletter

• Joachim,

Of all the references on the web, your Statistics Globe web site provides the most direct and clearest explanations of R commands and functions. Excellent work. I will explore your tutorials for both the R and Python programming environments. PR