Following this Stackoverflow question about how to get the diagonal vectors from a matrix (Get all diagonal vectors from matrix) I was stuck when trying to save one of the elements.
A <- matrix(1:16, 4)
d <- row(A) - col(A)
d.chem <- split(A, d)
d.chem
# $`-3`
# [1] 13
#
# $`-2`
# [1] 9 14
#
# $`-1`
# [1] 5 10 15
#
# $`0`
# [1] 1 6 11 16
#
# $`1`
# [1] 2 7 12
#
# $`2`
# [1] 3 8
#
# $`3`
# [1] 4
I would like to save just this element: $-1
. How can I do it?
I tried the following but I got an error message:
lapply(d.chem, '[[', 3)
#Error in FUN(X[[i]], ...) : subscript out of bounds
unlist(lapply(d.chem, '[[', 3))
#Error in FUN(X[[i]], ...) : subscript out of bounds