** col()** function in R Language is used to get a matrix which contains the number of columns of the matrix passed to it as argument.

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

**Example 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) ` |

*chevron_right*

*filter_none*

**Output:**

[, 1] [, 2] [1, ] 1 3 [2, ] 2 4 [, 1] [, 2] [1, ] 1 2 [2, ] 1 2

**Example 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) ` |

*chevron_right*

*filter_none*

**Output:**

[, 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

## Recommended Posts:

- Get the position of the maximum element in each Row of a Matrix in R Programming - max.col() Function
- Calculate the Sum of Matrix or Array columns in R Programming - colSums() Function
- Naming Rows and Columns of a Matrix in R Programming - rownames() and colnames() Function
- Scale the Columns of a Matrix in R Programming - scale() Function
- Get the number of columns of an Object in R Programming - ncol() Function
- Getting a Matrix of number of rows in R Programming - row() Function
- Merge Two Data Frames by common Columns in R Programming - merge() Function
- Rename Columns of a Data Frame in R Programming - rename() Function
- Choose Specific Columns of a Data Frame in R Programming - select() Function
- Combine Vectors, Matrix or Data Frames by Columns in R Language - cbind() Function
- Getting the Modulus of the Determinant of a Matrix in R Programming - determinant() Function
- Getting the Determinant of the Matrix in R Programming - det() Function
- Check if the Object is a Matrix in R Programming - is.matrix() Function
- Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function
- Convert an Object into a Matrix in R Programming - as.matrix() Function
- Transform the Scaled Matrix to its Original Form in R Programming - Using Matrix Computations
- Binding rows and columns of a Data Frame in R - bind_rows() and bind_cols() Function
- Getting and Setting Length of the Vectors in R Programming - length() Function
- Getting Match of an Element within a Vector in R Programming - charmatch() Function
- Getting class of different data types in R Programming - class() Function

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.