# Getting a Matrix of number of columns in R Programming – col() Function

• Last Updated : 03 Jun, 2020

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

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

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
```
My Personal Notes arrow_drop_up