find the first element in an array in r

To find the first element of an array in R, we can use indexing. In R, arrays are created using the array() function, and indexing starts from 1 instead of 0.

Here's a simple example:

main.r
# create an array
my_array <- array(1:12, dim = c(3, 4, 1))

# get the first element
first_element <- my_array[1, 1, 1]

# print the result
print(first_element)
161 chars
9 lines

The result will be:

main.r
[1] 1
6 chars
2 lines

In the example, we created a 3-dimensional array with dimension 3 x 4 x 1. We then used indexing to get the first element of the array, which is located at index [1, 1, 1]. We stored the result in the variable 'first_element' and printed it to the console.

gistlibby LogSnag