Convert an Object into a Matrix in R Programming – as.matrix() Function

as.matrix() function in R Language is used to convert an object into a Matrix.

Syntax: as.matrix(x)

Parameters:
x: Object to be converted

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to convert an object to matrix
  
# Creating a vector
x <- c(1:9)
  
# Calling as.matrix() Function
as.matrix(x)

chevron_right


Output:

      [, 1]
 [1, ]    1
 [2, ]    2
 [3, ]    3
 [4, ]    4
 [5, ]    5
 [6, ]    6
 [7, ]    7
 [8, ]    8
 [9, ]    9

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to convert an object to matrix
  
# Calling pre-defined data set
BOD
  
# Calling as.matrix() Function
as.matrix(BOD)

chevron_right


Output:

  Time demand
1    1    8.3
2    2   10.3
3    3   19.0
4    4   16.0
5    5   15.6
6    7   19.8
     Time demand
[1, ]    1    8.3
[2, ]    2   10.3
[3, ]    3   19.0
[4, ]    4   16.0
[5, ]    5   15.6
[6, ]    7   19.8
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.