R

# Which() Function in R

In R, the which() function is used to determine the indices of the elements of a vector or a matrix that satisfy a specified condition. It returns the numeric indices of the values that are TRUE in the logical condition set in its argument. Moreover, the which() function is particularly useful to subset or retrieve the data from vectors, matrices, or DataFrames. With this piece of article, we will explore more about the which() function of R. We provided some examples of the which() function in different ways to illustrate its usage and implementation.

## Example 1: Using the Which() Function on Vectors

As we previously mentioned, the which() function applies to vectors. So, we call the which() function to take the vectors and get the index values.

vec <- c(9, 1, 7, 5, 11, 3)

which(vec == 7)

which(vec == 3)

which(vec == 9)

which(vec > 5)

The code of R is declared with the “vec” variable where the six numeric vectors are specified using the c() function. After that, we apply the which() function on these vectors to get their position in the vector set. For this, we pass the condition first for the “seven” vector to get its position. Then, the which() function returns the index values for the value of “7” here.

Similarly, for the value of “3” in the vector set, we set the same condition in the which() function as we did in the previous vector. This gets the index position of the value of “3”. Lastly, to find the position of the elements that are greater than 5 in the vector, we set the “greater than a” condition in the which() function.

The output shows the specified elements’ positions for the corresponding conditions: ## Example 2: Using the Which() Function to Count the Occurrence of Values

However, we can also use the which() function to count the frequency of the elements in the vectors. The following code is used to count the frequency of the specified value using the which() function:

numbers <- c(11, 12, 12, 13, 14, 14, 15, 15, 16, 17)

length(which(numbers > 15))

The code of R is set with the vectors that contain the integer values in the c() function. After that, we use the length() function where the which() function is invoked. The which() function is then specified with the “numbers > 15” expression that returns a vector which contains the indices of the elements in the data that are greater than “15”. Finally, the length() function totals the number of elements in the vector that is returned by the which() function expression.

Hence, the output is retrieved which shows the value of “2” which is the index of elements that is greater than “15” in the given vector: ## Example 3: Using the Which() Function on Matrices

Next, we use the which() function with the arguments. The which() function is applied to the matrices in this particular section.

Matrix <- matrix(2:10, 3, 3)

Matrix

which(Matrix %% 2 == 0, arr.ind = TRUE)

The code of R is defined with the “Matrix” variable where the “3×3” matrix is created using the matrix() function. The values of the matrix are set between the range from “2” to “10”. Then, we employ the which() function and pass the expression inside it. The “Matrix %% 2 == 0” expression returns a logical matrix of the same dimensions as “Matrix”, with TRUE at the positions where the corresponding elements in “Matrix” are even, and FALSE elsewhere.

Then, the which() function is provided with the “arr.ind” argument which is set with the TRUE value. The “arr.ind” argument indicates that the function should return the indices in the matrix.

The output displays the matrix with the row and column indices of the elements that are even: ## Example 4: Using the Which() Function on the DataFrames

Additionally, the indices of the DataFrames can also be identified using the which() function. The program of the which() function to a DataFrame is given as follows:

data <- data.frame(z1 = c(1, 2, 3, 3, 4, 5),

z2 = c('A', 'B', 'C', 'D', 'E', 'F'))

data[which.max(data\$z1), ]

data[which.min(data\$z1), ]

The code of R is declared with the “df” variable where the data.frame() function is defined and set with the columns “col1”, “col2”, and “col3”. Here, “col1” column contains the numeric value with the range from 1 to 4. The “col2” column contains the first four letters and the “col3” column contains the constant value of “4”.

After that, we have the which() function with the “colnames(df) %in% c(“col1”, “col3”)” expression which evaluates to a vector of indices that corresponds to the given column in the DataFrame that is present in the c(“col1”, “col3”) vector. The %in% operator here checks which column names in the DataFrame are also present in the specified vector.

Hence, the following output generates a new DataFrame that contains all rows from df, but only the specified columns: ## Example 5: Using the Which() Function to Get the Min and Max Values

Conversely, we can identify the maximum and minimum values of the rows using the “which.max” and “which.min”, respectively.

df <- data.frame(col1 = 1:4,

col2 = letters[1:4],

col3 = 4)

df[ , which(colnames(df) %in% c("col1", "col3"))]

The code of R is provided with the DataFrame by calling the data.frame() function inside the “data” variable. The DataFrame contains two columns -“z1” and “z2”. The “z1” column is set with the first five integer values whereas the “z2” column is set with the first five alphabets. After that, we need to figure out the maximum value from the “data” DataFrame in “z1” column. For this, we deploy the “which.max()” function to identify the index of the maximum value in the “z1” column of the DataFrame.

The result is then used to subset the DataFrame and retrieve the row with the maximum value in the “z1” column. Next, we get the minimum value from the “z2” column which has alphabets. So, we call the “which.min” function to get the index of the lowest value in the DataFrame’s “z2” column. The output retrieves the row with the minimum value in the “z2” column.

The following output displays the maximum value first with the index and then the minimum value with the index: ## Conclusion

We worked with the which() function of R by demonstrating its implementation. We used the which() function over the vectors, matrices, and the DataFrame in the provided examples. Furthermore, we have seen the which() function with the arguments it provided and also get the maximum and minimum value index with the which() function. However, the which() function is the most effective in finding the indices of elements that meet certain conditions. 