as_matrix() coerces x to a matrix.

as_matrix(x, ...)

Arguments

x

An object to coerce to a matrix.

...

Objects passed on to methods.

Value

A matrix.

Details

1D arrays are coerced to 1 column matrices.

For a >2D object to be coercible to a matrix, all of the dimensions except for the first two must be size 1. Meaning an array with dimensions (3, 2, 1) would be coercible to a (3, 2) matrix, but one with (3, 1, 2) would not be.

See also

Examples

as_matrix(rray(1:10))
#> [,1] #> [1,] 1 #> [2,] 2 #> [3,] 3 #> [4,] 4 #> [5,] 5 #> [6,] 6 #> [7,] 7 #> [8,] 8 #> [9,] 9 #> [10,] 10
# >2D structures can be coerced to matrices # their first and second dimensions are # the only ones having a size >1 x <- rray(1, c(2, 2, 1)) as_matrix(x)
#> [,1] [,2] #> [1,] 1 1 #> [2,] 1 1
# This cannot be coerced to a matrix y <- rray_reshape(x, c(2, 1, 2)) try(as_matrix(y))
#> Error : A >2D object can only be reduced to a matrix if all dimensions except for the first two are 1. This is not the case for dimension(s): 3. #> Backtrace: #> █ #> 1. ├─base::tryCatch(...) #> 2. │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> 3. │ ├─base:::tryCatchOne(...) #> 4. │ │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 5. │ └─base:::tryCatchList(expr, names[-nh], parentenv, handlers[-nh]) #> 6. │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> 7. │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 8. ├─base::withCallingHandlers(...) #> 9. ├─base::saveRDS(...) #> 10. ├─base::do.call(...) #> 11. ├─(function (what, args, quote = FALSE, envir = parent.frame()) ... #> 12. └─(function (..., crayon_enabled, crayon_colors, pkgdown_internet) ... #> 13. └─pkgdown::build_site(...) #> 14. └─pkgdown:::build_site_local(...) #> 15. └─pkgdown::build_reference(...) #> 16. └─purrr::map(...) #> 17. └─pkgdown:::.f(.x[[i]], ...) #> 18. └─pkgdown:::data_reference_topic(...) #> 19. └─pkgdown:::run_examples(...) #> 20. └─pkgdown:::highlight_examples(code, topic, env = env) #> 21. └─evaluate::evaluate(x, child_env(env), new_device = TRUE) #> 22. └─evaluate:::evaluate_call(...) #> 23. ├─evaluate:::timing_fn(...) #> 24. ├─evaluate:::handle(...) #> 25. │ └─base::try(f, silent = TRUE) #> 26. │ └─base::tryCatch(...) #> 27. │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> 28. │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> 29. │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 30. ├─base::withCallingHandlers(...) #> 31. ├─base::withVisible(eval(expr, envir, enclos)) #> 32. └─base::eval(expr, envir, enclos) #> 33. └─base::eval(expr, envir, enclos) #> 34. ├─base::try(as_matrix(y)) #> 35. │ └─base::tryCatch(...) #> 36. │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> 37. │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> 38. │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 39. ├─rray::as_matrix(y) #> 40. └─rray:::as_matrix.vctrs_rray(y) #> 41. └─rray:::validate_matrix_coercible_dim(dim) #> 42. └─rray:::glubort(...)