Get or Set Dimensions of a Matrix in R Programming – dim() Function

dim() function in R Language is used to get or set the dimension of the specified matrix, array or data frame.

Syntax: dim(x)

Parameters:
x: array, matrix or data frame.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# dim function
  
# Getting R Biochemical Oxygen Demand Dataset
BOD 
  
# Getting dimension of the above dataset
dim(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

[1] 6 2

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# dim function
  
# Getting the number from 1 to 9
x <- rep(1:9)
x
  
# Calling the dim() function to
# Set dimension of 3 * 3
dim(x) <- c(3, 3)
  
# Getting the numbers
# in 3 * 3 representation
x

chevron_right


Output:

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

     [, 1] [, 2] [, 3]
[1, ]    1    4    7
[2, ]    2    5    8
[3, ]    3    6    9
My Personal Notes arrow_drop_up

Recommended Posts:


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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.