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.
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:
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.
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:
z2 = c('A', 'B', 'C', 'D', 'E', 'F'))
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.
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:
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.