which()
function in R Language is used to return the indices of the object which return true for the logical operation passed as argument.
Syntax: which(x, arr.ind)
Parameters:
x: logical object
arr.ind: Boolean value to display indices
Example 1:
x < - matrix( 1 : 9 , 3 , 3 )
x
which(x % % 2 = = 0 , arr.ind = TRUE)
|
Output:
[, 1] [, 2] [, 3]
[1, ] 1 4 7
[2, ] 2 5 8
[3, ] 3 6 9
row col
[1, ] 2 1
[2, ] 1 2
[3, ] 3 2
[4, ] 2 3
Here, in the above code, the which()
function returns the indices of all the even numbers present in the matrix.
Example 2:
BOD
which(BOD$demand = = 19 , arr.ind = TRUE)
|
Output:
Time demand
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8
[1] 3
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!