当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R语言 ncol()用法及代码示例


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

用法: ncol(x)
参数: 
x: matrix, vector, array or data frame 
 

范例1:

Python3


# 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:

Python3


# 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

相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Get the number of columns of an Object in R Programming – ncol() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。