*25*

The **which()** function in R returns the position of elements in a logical vector that are **TRUE**.

This tutorial provides several examples of how to use this function in practice.

**Example 1: Find Elements in a Vector**

The following code shows how to find the position of all elements in a vector that are equal to 5:

#create data data #find the position of all elements equal to 5 which(data == 5) [1] 8 9

We can see that the elements in positionsÂ **8** and **9** in the vector are equal to the value 5.

We can also find the position of all elements in a vector that are *not* equal to 5:

#find the position of all elementsnotequal to 5 which(data != 5) [1] 1 2 3 4 5 6 7 10

We can also find which elements are between two values or outside of two values:

#find the position of all elements with values between 2 and 4 which(data >= 2 & data 4) [1] 2 3 4 5 6 7 #find the position of all elements with values outside of 2 and 4 which(data 2 | data > 4) [1] 1 8 9 10

**Example 2: Count Occurrences in a Vector**

The following code shows how to use the **length()** function to find the number of elements in a vector that are greater than some value:

#create data data #find number of elements greater than 4 length(which(data > 4)) [1] 3

We can see that there are 3 elements in this vector with values greater than 4.

**Example 3: Find Rows in a Data Frame**

The following code shows how to return the row in a data frame that contains the max or min value in a certain column:

#create data frame df frame(x = c(1, 2, 2, 3, 4, 5), y = c(7, 7, 8, 9, 9, 9), z = c('A', 'B', 'C', 'D', 'E', 'F')) #view data frame df x y z 1 1 7 A 2 2 7 B 3 2 8 C 4 3 9 D 5 4 9 E 6 5 9 F #return row that contains the max value in columnxdf[which.max(df$x), ] x y z 6 5 9 F #return row that contains the min value in columnxdf[which.min(df$x), ] x y z 1 1 7 A

**Example 4: Subset by Rows in a Data Frame**

The following code shows how to subset a data frame by rows that meet a certain criteria:

#create data frame df frame(x = c(1, 2, 2, 3, 4, 5), y = c(7, 7, 8, 9, 9, 9), z = c('A', 'B', 'C', 'D', 'E', 'F')) #view data frame df x y z 1 1 7 A 2 2 7 B 3 2 8 C 4 3 9 D 5 4 9 E 6 5 9 F #return subset of data frame where values in column y are greater than 8 df[which(df$y > 8), ] x y z 4 3 9 D 5 4 9 E 6 5 9 F

Find more R tutorials on this page.