colnames
Column and Row Names
Description
Extracts or replaces column and row names for matrix and array objects.
Usage
colnames(x, do.NULL = TRUE, prefix = "col")
colnames(x) <- value
rownames(x, do.NULL = TRUE, prefix = "row")
rownames(x) <- value
Arguments
x |
a matrix or array object.
|
do.NULL |
a logical value. If FALSE, the function returns the created names
if the names for the column (colnames) or row (rownames)
are NULL. If TRUE (the default), the function returns the
names for the column (colnames) or row (rownames), if
they exist; if they are NULL, it returns NULL.
|
prefix |
a character string to use as a prefix when creating row or column names.
|
Details
If object x has dimnames, then the first component is
returned as row names and, if the second component exists, it is returned
as column names.
If do.NULL = FALSE, you must specify new column names or row
names using the prefix argument.
When you use value to set the column or row names, value
must be a character vector.
If x is a matrix, the length of the character vector that you
specify in value must match the number of columns for
colnames or the number of rows for rownames present
in the matrix x.
If
x is a data frame, when you set the names, observe the following:
- column: the length of value must be less than or equal to
the number of columns in x.
- row: value must contain unique values and must not contain
missing values (NA). The length must be equal to the number of
rows in x.
Value
rownames | returns the first element in the dimnames (if it
exists) for the object. |
colnames | returns the second element in the dimnames (if it
exists) for the object. |
If
dimnames does not exist, or if the element in
dimnames
is
NULL and
do.NULL = FALSE, then a character vector of
the appropriate length is created by prepending
prefix to a
sequence of integers beginning with "1".
See Also
Examples
m <- matrix(1:6, 3, 2)
colnames(m)
# NULL
rownames(m)
# NULL
rownames(m) <- c("R1", "R2", "R3")
# Generate names when there are none
colnames(m, do.NULL = FALSE, prefix = "C")
# [1] "C1" "C2"
rownames(m) # R1, R2, R3
# [1] "R1" "R2" "R3"