OneBite.Dev - Coding blog in a bite size

get the last element of array in R

Code snippet on how to get the last element of array in R

  x <- c(1, 2, 3, 4, 5)
  last_element <- x[length(x)]

This code is used to retrieve the last element of an array in R. First, the array is created using the c() command, with each element of the array separated by a comma between parenthesis. In this example, the array consists of the numbers 1 through 5. Next, the length of the array is retrieved using the length() command, which returns the number of elements in the array (in this case, 5 elements) as an integer. Finally, the last element of the array is set to a variable called last_element using the array indexing function, with the length of the array supplied as the argument. This returns the last element of the array, which in this case is 5.

r