find the last element in an array in r

To find the last element in an array in R, you can use indexing or the tail() function. Here are two examples:

# Create an array
my_array <- c(1, 2, 3, 4, 5)

# Using indexing
last_element <- my_array[length(my_array)]
print(last_element)
# Output: 5

# Using tail function
last_element <- tail(my_array, n=1)
print(last_element)
# Output: 5
231 chars
13 lines

In both cases, you're using length(my_array) to get the number of elements in the array and then indexing that position to get the last element in the array.

related categories

gistlibby LogSnag