Getting a Matrix of number of columns in R Programming – col() Function
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:
x < - matrix(c( 1 , 2 , 3 , 4 ), 2 , 2 )
x
col(x)
|
Output:
[, 1] [, 2]
[1, ] 1 3
[2, ] 2 4
[, 1] [, 2]
[1, ] 1 2
[2, ] 1 2
Example 2:
x < - matrix(rep( 1 : 9 ), 3 , 3 )
x
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
Last Updated :
03 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...