R语言 获取或设置矩阵的尺寸 – dim()函数
R语言中的 dim() 函数用于获取或设置指定矩阵、数组或数据帧的尺寸。
语法: dim(x)
参数:
x: 数组、矩阵或数据框。
例1 :
# R program to illustrate
# dim function
# Getting R Biochemical Oxygen Demand Dataset
BOD
# Getting dimension of the above dataset
dim(BOD)
输出
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
例2 :
# 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
输出
[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