[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  row+colnames: Row and Column Names

##  Description

Retrieve or set the row or column names of a matrix-like object.

##  Usage

    
    
    rownames(x, do.NULL = TRUE, prefix = "row")
    rownames(x) <- value
    
    colnames(x, do.NULL = TRUE, prefix = "col")
    colnames(x) <- value

##  Arguments

x

a matrix-like  R  object, with at least two dimensions for ` colnames ` .

do.NULL

logical. If ` FALSE ` and names are ` NULL ` , names are created.

prefix

for created names.

value

a valid value for that component of ` [ dimnames
](/link/dimnames?package=base&version=3.6.2) (x) ` . For a matrix or array
this is either ` NULL ` or a character vector of non-zero length equal to the
appropriate dimension.

##  Details

The extractor functions try to do something sensible for any matrix-like
object ` x ` . If the object has ` [ dimnames
](/link/dimnames?package=base&version=3.6.2) ` the first component is used as
the row names, and the second component (if any) is used for the column names.
For a data frame, ` rownames ` and ` colnames ` eventually call ` [ row.names
](/link/row.names?package=base&version=3.6.2) ` and ` [ names
](/link/names?package=base&version=3.6.2) ` respectively, but the latter are
preferred.

If ` do.NULL ` is ` FALSE ` , a character vector (of length ` [ NROW
](/link/NROW?package=base&version=3.6.2) (x) ` or ` [ NCOL
](/link/NCOL?package=base&version=3.6.2) (x) ` ) is returned in any case,
prepending ` prefix ` to simple numbers, if there are no dimnames or the
corresponding component of the dimnames is ` NULL ` .

The replacement methods for arrays/matrices coerce vector and factor values of
` value ` to character, but do not dispatch methods for ` as.character ` .

For a data frame, ` value ` for ` rownames ` should be a character vector of
non-duplicated and non-missing names (this is enforced), and for ` colnames `
a character vector of (preferably) unique syntactically-valid names. In both
cases, ` value ` will be coerced by ` [ as.character
](/link/as.character?package=base&version=3.6.2) ` , and setting ` colnames `
will convert the row names to character.

##  See Also

` [ dimnames ](/link/dimnames?package=base&version=3.6.2) ` , ` [ case.names
](/link/case.names?package=base&version=3.6.2) ` , ` [ variable.names
](/link/variable.names?package=base&version=3.6.2) ` .

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Frow%2Bcolnames&utm_source=r-docs&utm_medium=docs&utm_term=row+colnames&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    m0 <- matrix(NA, 4, 0)
    rownames(m0)
    
    m2 <- cbind(1, 1:4)
    colnames(m2, do.NULL = FALSE)
    colnames(m2) <- c("x","Y")
    rownames(m2) <- rownames(m2, do.NULL = FALSE, prefix = "Obs.")
    m2
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Frow%2Bcolnames&utm_source=r-docs&utm_medium=docs&utm_term=row+colnames&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)