Ensure that input is matrix (by as.matrix) and check number of rows (and columns)
Source:R/EnsureIntercept.R
EnsureMatrix.Rd
Ensure that input is matrix (by as.matrix) and check number of rows (and columns)
Examples
x <- matrix(c(5, 8, 4, 2, 7, 6), 3, 2)
EnsureMatrix(x)
#> [,1] [,2]
#> [1,] 5 2
#> [2,] 8 7
#> [3,] 4 6
EnsureMatrix(x, 3)
#> [,1] [,2]
#> [1,] 5 2
#> [2,] 8 7
#> [3,] 4 6
EnsureMatrix(1:4)
#> [,1]
#> [1,] 1
#> [2,] 2
#> [3,] 3
#> [4,] 4
EnsureMatrix(1:4, 4)
#> [,1]
#> [1,] 1
#> [2,] 2
#> [3,] 3
#> [4,] 4
EnsureMatrix(NULL, 4)
#>
#> [1,]
#> [2,]
#> [3,]
#> [4,]
try(EnsureMatrix(x, 4))
#> Error in EnsureMatrix(x, 4) : nrow is 3 when 4 expected
try(EnsureMatrix(1:3, 4))
#> Error in EnsureMatrix(1:3, 4) : nrow is 3 when 4 expected
EnsureMatrix(x, 3, 2)
#> [,1] [,2]
#> [1,] 5 2
#> [2,] 8 7
#> [3,] 4 6
try(EnsureMatrix(x, 3, 3))
#> Error in EnsureMatrix(x, 3, 3) : ncol is 2 when 3 expected
try(EnsureMatrix(NULL, 3, 3))
#> Error in EnsureMatrix(NULL, 3, 3) : nCol not allowed when x is NULL