Search code examples
rlisttail

Obtaining the tails of different lengths from sub-lists


My problem is that I have a list with sub-lists and I want to obtain the tails of each sub-list, but these should be of different sizes.

For example, I would like to get the tail of length n=10 for sub-list a and the tail of length n=5 for sub-list b (see code below). How to best do this?

myList <- list(list(a=replicate(10, rnorm(20)), b=replicate(10, rnorm(10))))


Solution

  • Something like this:

    myTail <- c(10, 5)
    lapply(1:length(myTail), function(x) tail(unlist(myList[[1]][x]), myTail[x]))