diag(1.1)
#> [,1]
#> [1,] 1
diag(0.9)
#> <0 x 0 matrix>
diag(0.9, nrow = 1)
#> [,1]
#> [1,] 0.9
diag(-1)
#> Error in diag(-1): invalid 'nrow' value (< 0)
# oh, it's treating this as the nrow arg?
diag(x = -1)
#> Error in diag(x = -1): invalid 'nrow' value (< 0)
# ugh
diag(-1, nrow = 1)
#> [,1]
#> [1,] -1
Last active
January 3, 2018 20:22
-
-
Save ClaytonJY/f663a9462a29669f79faad6d2eeaec6c to your computer and use it in GitHub Desktop.
R's diag function does weird things
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem is that
diag
tries to do too many things:compare to a language like Julia where there are separate functions for separate uses
diag(M, k::Integer=0)
diagm(v, k::Integer=0)
Diagonal(A::AbstractMatrix)
So when you use
diag(-1)
ordiag(0.9)
in R what do you expect?