Get Indices of Specified Values of an Array in R Programming – arrayInd() Function
Last Updated :
08 Jun, 2020
arrayInd()
function in R Language is used to get the indices of the values passed to the function as argument. This function takes values and the array in which the values are to be searched and returns the indices for each match found.
Syntax: arrayInd(values, dim(x))
Parameters:
values: value or vector of values to be searched
dim(x): array to be searched
x: Array name
Example 1:
x < - array( 1 : 9 , dim = c( 2 , 3 ))
x
x1 < - c( 5 , 4 , 6 )
arrayInd(x1, dim(x))
|
Output:
[, 1] [, 2] [, 3]
[1, ] 1 3 5
[2, ] 2 4 6
[, 1] [, 2]
[1, ] 1 3
[2, ] 2 2
[3, ] 2 3
Example 2:
x < - array( 1 : 9 , dim = c( 3 , 3 ))
x
x1 < - which(x > 3 & x < 8 )
arrayInd(x1, dim(x))
|
Output:
[, 1] [, 2] [, 3]
[1, ] 1 4 7
[2, ] 2 5 8
[3, ] 3 6 9
[, 1] [, 2]
[1, ] 1 2
[2, ] 2 2
[3, ] 3 2
[4, ] 1 3
Here, in tonhte above code, the arrayInd()
function returns the indices of all the values which were returned by the which()
function.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...