which() Function in R - GeeksforGeeks

23 Aug, 2021
which() function in R Programming Language is used to return the position of the specified values in the logical vector.
Syntax: which(x, arr.ind, useNames)
Parameters: This function accepts some parameters which are illustrated below:
X: This is the specified input logical vector
Arr.ind: This parameter returns the array indices if x is an array.
useNames: This parameter says the dimension names of an array.
Return value: This function returns the position of the specified values in the logical vector.
In the below example, which() function returns the alphabetical position of the specified letter. For example, a is the first letter in the alphabet sequence that’s why 1 is returned and z is the last letter in the sequence so 26 is returned.
R
# R program to illustrate# which() function  # Calling the which function# to return alphabetical position# of the given alphabetwhich(letters == "a")which(letters == "d")which(letters == "z")which(letters == "p")which(letters == "g")
Output :
[1] 1
[1] 4
[1] 26
[1] 16
[1] 7
In the below example, the position of some elements of the specified vector is being returned which the help of which() function.
R
# R program to illustrate# which() function  # Creating a vector of some elementsvector <- c(3, 5, 1, 6, 12, 4)  # Getting the position of element 12# in the above vectorwhich(vector == 12)  # Getting the position of element 1# in the above vectorwhich(vector == 1)  # Getting the position of element 6# in the above vectorwhich(vector == 6)  # Getting the position of elements# those are greater than 5which(vector > 5)
Output:
[1] 5
[1] 3
[1] 4
[1] 4 5
 In the below example, which() function is used to find the columns in a data frame with numeric values.
An Iris data set is used as a data frame that contains 4 columns for numerical values and 1 column for categorical values i.e., Species. The which() function find the columns name from the data set that contain numeric values.
R
# Considering “Iris” datasetdata_set <- datasets::iris  # Printing the Iris dataset values# along with its 5 columns out of which# 4 columns are numerical and 1 is categorical# (Species)head(data_set)  # Calling the which() function over# the above specified data set that # returns the columns with numeric valuesResult <- which(sapply(data_set, is.numeric))  # Printing the columns with numeric valuescolnames(data_set)[Result]
Output:
In the below example, which() function is used to find the position of an element in the specified matrix.
Here the position of value 2 in the specified matrix is being calculated.
R
# Creating a matrix of 3 columns and 4 rowsMatrix <- matrix(rep(c(1, 2, 3), 4), nrow = 4)  # Printing the entire matrix with its valuesMatrix  # Calling the which() function to find the # position of value 2 in the above matrixwhich(Matrix == 2, arr.ind = T)
Output:
Picked
R-Functions
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to Change Axis Scales in R Plots?
Replace Specific Characters in String in R
How to filter R dataframe by multiple conditions?
R - if statement
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
Time Series Analysis in R