R语言 获取一个对象的列数 – ncol()函数

R语言 获取一个对象的列数 – ncol()函数

R语言中的 ncol() 函数用于返回指定矩阵的列数。

语法: ncol(x)

参数:

x: 矩阵、向量、数组或数据框

例1:

# R program to illustrate
# ncol function
 
# Getting R Biochemical Oxygen Demand Dataset
BOD
 
# Calling ncol() function to
# get the number of columns
ncol(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] 2

例2:

# R program to illustrate
# ncol function
 
# Specifying a matrix
x <- matrix(c(1, 2, 3, 4), 2, 2)
 
# Calling the ncol() function
ncol(x)

输出:

[1] 2

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程