I am trying to get the value of the last element of every subelement of a list in R. The elements of the list are vectors of different length, so the index
You can use lapply and tail:
lapply
tail
x <- list(a=1:5, b = 1:10, d = 9:11) last_elems <- lapply(x, tail, n = 1L)
As pointed out by Hadd E. Nuff, to obtain a numeric vector instead of a one-element list, vapply is a good choice:
vapply
last_elems <- vapply(x, tail, n = 1L, FUN.VALUE = numeric(1))