I think this is the data structure
> l = list(list(1), list(2), list(31, 32), list(4))
> m = matrix(l, 2)
> m
[,1] [,2]
[1,] List,1 List,2
[2,] List,1 List,1
> class(m)
[1] "matrix"
> mode(m)
[1] "list"
You efficiently query for the length of each element
> lengths(m)
[1] 1 1 2 1
and then think of these as indexes into the unlisted m
> cumsum(c(1L, lengths(m)[-length(m)]))
Then actually do the unlisting (avoiding the cost of creating / copying names, if any) and subsetting
> unlist(m, use.names=FALSE)[cumsum(c(1L, lengths(m)[-length(m)]))]
[1] 1 2 31 4
This could be re-shaped into a matrix by adding the original dimensions
> n = unlist(m, use.names=FALSE)[cumsum(c(1, lengths(m)[-length(m)]))]
> dim(n) = dim(m); dimnames(n) = dimnames(m); n
[,1] [,2]
[1,] 1 31
[2,] 2 4
An alternative would I guess be to vapply-subset (guessing that the content of NR is numeric())
n = vapply(m, `[[`, numeric(1), 1L)
I don't honestly know which is faster; maybe a microbenchmark() on real data would give some future guidance? Also there might be some built-in cleverness for the cumsum() step, since it seems like it would be common in S4Vectors land.
Hmm, it seems like there are at least two other structures that are consistent with your description, and these pose different challenges
l = list(list(1), list(2), list(c(31, 32)), list(4))
l = list(1, 2, c(31, 32), 4)
Maybe a bit of clarification on the actual data structure?