The rownames()
and colnames()
functions in R are used to obtain or set the names of the row and column of a matrix-like object, respectively.
rownames(x, do.NULL = TRUE, prefix = "row")
rownames(x) <- value
colnames(x, do.NULL = TRUE, prefix = "col")
colnames(x) <- value
The rownames()
and colnames()
functions take the following parameter values:
x
: This is a matrix-like object that has at least two dimensions.do.Null
: This takes a logical value. If FALSE
and names are NULL
, names are created.prefix
: This represents the names that are created.value
: This is a valid value for that component of dimnames(x)
(that is, the values must be of the same dimension as the dimension of the matrix-like object). For a given array or matrix, this is either NULL
or a character vector of non-zero length equal to the appropriate dimension.The rownames()
and colnames()
functions both return a matrix object.
# creating the matrix objectscol1 <- c( 1 , 2 , 3 )col2 <- c("Lion", "Tiger", "Leopard")# conbining the matrix objectsc <- cbind(col1, col2)# setting the row and column namescolnames(c) <- c("Num", "Animal")rownames(c) <- c("row1", "row2", "row3")rownames(c) <- rownames(c, do.NULL=FALSE)# printing the matrixc# obtaining the column names of the matrixpaste(colnames(c))# obtaining the row names of the matrixpaste(rownames(c))
col1
and col2
.cbind()
function. Then we assign the result to a variable c
.colnames()
function.rownames()
function.rownames()
function on the variable c
.c
.colnames()
function.colnames()
function.