To get the last element in an R list, you can use negative indexing or the `length()` function with the index `[length(list)]`.
In this example,
numbers
with integer elements.length(numbers)
.numbers <- c(10, 20, 30, 40, 50)
last_element <- numbers[length(numbers)]
print(paste('Last element is:', last_element))
Last element is: 50
In this example,
names
with string elements.length(names)
.names <- c('Alice', 'Bob', 'Charlie', 'David')
last_element <- names[length(names)]
print(paste('Last element is:', last_element))
Last element is: David
In this tutorial, we learned How to get Last Element in a List in R language with well detailed examples.