Modify Diagonal, Lower & Upper Triangular Part of Matrix (3 Examples) | lower.tri, upper.tri & diag
Вставка
- Опубліковано 7 лют 2025
- How to change the diagonal, lower, and upper triangular part of a matrix in the R programming language. More details: statisticsglob...
R code of this video:
my_mat <- matrix(1:16, ncol = 4) # Create example matrix
my_mat # Print example matrix
my_mat_new1 <- my_mat # Duplicate matrix
my_mat_new1[lower.tri(my_mat_new1)] <- 0 # Change lower triangular part
my_mat_new1 # Print updated matrix
my_mat_new2 <- my_mat # Duplicate matrix
my_mat_new2[upper.tri(my_mat_new2)] <- 101:106 # Change upper triangular part
my_mat_new2 # Print updated matrix
my_mat_new3 <- my_mat # Duplicate matrix
my_mat_new3[diag(my_mat_new3)] <- NA # Change diagonal of matrix
my_mat_new3 # Print updated matrix
Follow me on Social Media:
Facebook - Statistics Globe Page: / statisticsglobecom
Facebook - R Programming Group for Discussions & Questions: / statisticsglobe
Facebook - Python Programming Group for Discussions & Questions: / statisticsglobepython
LinkedIn - Statistics Globe Page: / statisticsglobe
LinkedIn - R Programming Group for Discussions & Questions: / 12555223
LinkedIn - Python Programming Group for Discussions & Questions: / 12673534
Twitter: / joachimschork
Music by bensound.com
Your series are really giving me ideas for time series analysis. Grüsse aus München
This is great to hear, glad it's useful! Grüße zurück aus Karlsruhe :)
🌷
Thanks Weihong
Disclaimer: I'm garbage with matrices and matrix functions.
This question relates to parameter index matrices (PIMs) in population modeling (specifically in RMark). Does the diagonal function allow you to offset by column? In the models I am thinking of, the lower triangle is always 0's but you might want the diagonal to all be 1 and the next diagonal to be 2 and so on.
Thanks!
Hey Douglas, unfortunately, I'm not an expert on this topic. However, I have recently created a Facebook discussion group where people can ask questions about R programming and statistics. Could you post your question there? This way, others can contribute/read as well: facebook.com/groups/statisticsglobe