R语言 获取矩阵的列数 – col()函数

R语言 获取矩阵的列数 – col()函数

R语言中的 col() 函数用于获得一个矩阵,其中包含作为参数传递给它的矩阵的列数。

语法: col(x, as.factor=FALSE)

参数:

x: 矩阵

as.factor: 一个逻辑值,表示该值是否应该以列标的因子(必要时创建)而不是数字的形式返回。

例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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程