R - lengths(): get length of element of a list efficiently

14 Aug 2019

Get the length of each element of a list or atomic vector (is.atomic) as an integer or numeric vector.

More efficient version of sapply(x, length) and similar *apply calls to length.

xx <- list(rep(1,5), rep(4,8),rep(2,10))

lengths(xx)
# [1]  5  8 10
[ R  function  list  ]