I have a list of matrices (actually a list column in a larger dataframe) from which I want to extract particular rows or columns. Given a simple list like this:
l <- list(
matrix(1:4, nrow = 2),
matrix(5:8, nrow = 2),
matrix(9:12, nrow = 2),
matrix(13:16, nrow = 2)
)
It's easy to get a single row by indexing like this:
> l[[2]][1,]
[1] 5 7
But how can I extract the nth row from every item in the list at once? Something like:
> l[[]][1,]
[1] 1 3
[2] 5 7
[3] 9 11
[4] 13 15
I have a feeling I want to use purrr::map
here somehow, but I can't quite wrap my head around how. Any hints?