# NOT RUN {
ldiag(list(0, "b"))
ldiag("a", nrow=3)
# This works
a <- ldiag(1:3)
diag(a) <- "a"
diag(a) <- list("a", 0, 0)
# ldiag() is a convenience function to replace having to
# write code like this
a <- matrix(list(0), 3, 3)
diag(a) <- list("a", 0, 0)
# diag() alone won't work because it cannot handle mixed number/char lists
# This turns the off-diagonals to character "0"
a <- diag(1:3)
diag(a) <- "a"
# This would turn our matrix into a list only (not a matrix anymore)
a <- diag(1:3)
diag(a) <- list("a", 0, 0)
# This would return NA on the diagonal
a <- diag("a", 3)
# }
Run the code above in your browser using DataLab