在 R 编程中获取列数矩阵 - col()函数
R 语言中的col()
函数用于获取一个矩阵,该矩阵包含作为参数传递给它的矩阵的列数。
Syntax: col(x, as.factor=FALSE)
Parameters:
x: matrix
as.factor: a logical value indicating whether the value should be returned as a factor of column labels (created if necessary) rather than as numbers
示例 1:
# R program to illustrate
# col function
# Initializing a matrix with
# 2 rows and 2 columns
x <- matrix(c(1, 2, 3, 4), 2, 2)
# Getting the matrix representation
x
# Calling the col() function
col(x)
输出:
[, 1] [, 2]
[1, ] 1 3
[2, ] 2 4
[, 1] [, 2]
[1, ] 1 2
[2, ] 1 2
示例 2:
# R program to illustrate
# col function
# Initializing a matrix with
# 3 rows and 3 columns
x <- matrix(rep(1:9), 3, 3)
# Getting the matrix representation
x
# Calling the col() function
col(x)
输出:
[, 1] [, 2] [, 3]
[1, ] 1 4 7
[2, ] 2 5 8
[3, ] 3 6 9
[, 1] [, 2] [, 3]
[1, ] 1 2 3
[2, ] 1 2 3
[3, ] 1 2 3