# 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.

Table of contents:

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 |

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 |

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 |

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 |

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 |

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 |

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 |

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 |

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.

*The YouTube video will be added soon.*

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

- Extract Row from Data Frame
- Extract Certain Columns of Data Frame
- Extract data.table Column as Vector Using Index Position
- Get Column Index in Data Frame by Variable Name
- Find Index of Maximum & Minimum Value of Vector & Data Frame Row
- Find Index of Element in Vector
- R Functions List (+ Examples)
- The R Programming Language

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.

**5**/

**5**(

**2**votes )

### Subscribe to my free statistics newsletter: