find the index of an element in an array in r

To find the index of an element in an array in R, you can use the which() function. The which() function returns the indices of the elements that satisfy a certain condition. Specifically, you can use it to find the index of an element that is equal to a certain value.

Here's an example code snippet to find the index of an element in an array:

main.r
# Create an array
my_array <- c(10, 20, 30, 40)
 
# Find the index of 30
index <- which(my_array == 30)
 
# Print the index
print(index)
137 chars
9 lines

In this code snippet, the which() function is used to find the index of the element 30 in the my_array array. The result is stored in the index variable, and then printed to the console.

The output of this code snippet is:

main.r
[1] 3
6 chars
2 lines

This means that the index of the element 30 in the my_array array is 3.

gistlibby LogSnag