Get the number of columns of an Object in R Programming – ncol() Function

ncol() function in R Language is used to return the number of columns of the specified matrix.

Syntax: ncol(x)

Parameters:
x: matrix, vector, array or data frame

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# ncol function
  
# Getting R Biochemical Oxygen Demand Dataset
BOD 
  
# Calling ncol() function to
# get the number of columns
ncol(BOD) 

chevron_right


Output:

  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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

[1] 2
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.