Which Function in R – which()

Which function in R, returns the indices of the logical object when it is TRUE. In other words, which() function in R returns the position or index of value when it satisfies the specified condition.  which() function gives you the position of elements of a logical vector that are TRUE. It can be a row number or column number or position in a vector.

Lets see an example for each

  • Which function for vector
  • Extracting row index and column index using which function in dataframe
  • Which function for matrix to extract the position of a value

 

Syntax of which function in R:

which(x, arr.ind = FALSE)
  • x – logical vector
  • ind- logical; should array indices be returned when xis an array?

Example of Which function in R:

# which function in R

which(letters=="z")
which(letters=="s")

Returns the position of “Z” and “S” in the letters object.so the output will be

[1] 26

[1] 19

 

Which function for Vector:

# which function in R for vector

x = c(1,3,5,7,8) which(x==3) which(x>=7)

Returns the index position of x=3 and X>=7 in the vector and .so the output will be

[1] 2

[1] 4    5

 

 

Which function in R for data frame:

Let’s create the dataframe to depict an example of which function.

df = data.frame (NAME =c ('Alisa','Bobby','jodha','jack','raghu','Cathrine', 'Alisa','Bobby','kumar','Alisa','jack','Cathrine'), Age = c (26,24,26,22,23,24,26,24,22,26,22,25), Score =c(85,63,55,74,31,77,85,63,42,85,74,78)) 

df

so the resultant dataframe will be

which function in R 11

Now lets see how to use which() function for data frame

# which function in R for a data frame

which(df$Age==22,arr.ind = FALSE)
which(df$Score==85,arr.ind = FALSE)

output:

[1] 4    9     11

[1] 1     7     10

 

Get Column position using which() function in R

which function to get the column position is shown below

 

# which function in R to get column position 

which(names(df)== 'Score') 

so the column position of the “Score” column by using which function will be

output:

[1]  3

 

Get the name of the numeric column using Which function

 

# which function in R to get numeric column names

check = which(sapply(df, is.numeric))
colnames(df)[check]

Step 1 :  sapply(df, is.numeric) returns FALSE TRUE  TRUE FALSE. It’s TRUE where variable is number else FALSE.

Step 2: which(sapply(df, is.numeric)) returns 2 3. Adding WHICH function returns the position in logical vectors.

Step 3: colnames(df)[check] returns “Age” and “Score”

so the result will be

which function in R 12

 

Get Row index using which() function in R

which function to get the row index as shown below

 
# which function in R to row index of the dataframe
which(df$Score == max(df$Score)) 

gets the row index of the which satisfies the column with maximum scores

output:

[1] 1   7   10

 

Get Row index using multiple conditions with which() function

which function to get the row index using multiple conditions is shown below

 
# which function in R to row index of the dataframe

which(df$Age == 26 & df$Score == 85) 

Gets the row index of the dataframe based on the above conditions

output:

[1] 1   7   10

Which function to get the count of Occurrences

which function to get the row index using multiple conditions is shown below

 
# which function in R to row index of the dataframe

length(which(df$Age == 26 & df$Score == 85)) 

Gets the count of occurrences which satisfies the above conditions

output:

[1]   3

 

 

 

Which() function in R for matrix:

First lets generate the matrix

# matrix creation

m = matrix(rep(c(-1, 0, 1), 4), nrow = 4)

the resultant matrix will be

which function in R 13

 

# which function in R for matrix

which(m==0,arr.ind=TRUE)

It will return array index of the matrix when m==0 so the output will be
which function in R 14
 

previous small which function in r                                                                                                           next small which function in r