Grab triangles within a lower triangle

Posted by Tyler Rinker on Stack Overflow See other posts from Stack Overflow or by Tyler Rinker
Published on 2012-12-07T03:43:02Z Indexed on 2012/12/07 5:04 UTC
Read the original article Hit count: 245

Filed under:

I have the need to grab all the thee element triangles that make up the lower triangle of a symmetric matrix. I can not think of how to grab all these pieces in the order of far left column working down and then next column to the right and so on. I know that the numbe rof mini triangles inside of the lower triangle is:

n = x(x - 1)/2
where: x = nrow(mats[[i]])

Here I've created three matrices with letters (it's easier for me to conceptualize this way) and the elements in the order I'm looking for:

FUN <- function(n) {
    matrix(LETTERS[1:(n*n)], n)
}

mats <- lapply(3:5, FUN)

So this is the output I'd like to get (I put it in code rather than output format) for each of the matrices created above:

list(c("B", "C", "F"))

list(c("B", "C", "G"), c("C", "D", "H"), c("G", "H", "L"))

list(c("B", "C", "H"), c("C", "D", "I"), c("D", "E", "J"), 
    c("H", "I", "N"), c("I", "J", "O"), c("N", "O", "T"))

How can I do this task in the fastest manner possible while staying in base R?

Not sure if this visual of what I'm after is helpful but it may be:

enter image description here

© Stack Overflow or respective owner

Related posts about r