Open In App

Get Indices of Specified Values of an Array in R Programming – arrayInd() Function

Last Updated : 08 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:




# R program to illustrate 
# the use of arrayInd() function
  
# Creating an array
x <- array(1:9, dim = c(2, 3)) 
x
  
# Creating vector of values to be found
x1 <- c(5, 4, 6)
  
# Calling arrayInd() function
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:




# R program to illustrate 
# the use of arrayInd() function
  
# Creating an array
x <- array(1:9, dim = c(3, 3)) 
x
  
# Extracting values using which() function
x1 <- which(x > 3 & x < 8)
  
# Calling arrayInd() function
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
Previous
Next
Share your thoughts in the comments

Similar Reads