Retrieve or set the dimnames of an object.
1 2 3 4
an R object, for example a matrix, array or data frame.
a possible value for
a character string, used to separate
logical indicating that the dimnames constructed are
unique within each dimension in the sense of
dimnames<- are generic.
array (and hence in particular, for a
matrix), they retrieve or set the
attribute (see attributes) of the object. A list
value can have names, and these will be used to label the
dimensions of the array where appropriate.
The replacement method for arrays/matrices coerces vector and factor
value to character, but does not dispatch methods
as.character. It coerces zero-length elements to
NULL, and a zero-length list to
is a list shorter than the number of dimensions, it is extended with
NULLs to the needed length.
Both have methods for data frames. The dimnames of a data frame are
row.names and its
names. For the
replacement method each component of
value will be coerced by
For a 1D matrix the
names are the same thing as the
(only) component of the
Both are primitive functions.
“missing”, such that its result has
dimnames for each component. If
unique is true as by default,
they are unique within each component via
The dimnames of a matrix or array can be
NULL (which is not
stored) or a list of the same length as
dim(x). If a list, its
components are either
NULL or a character vector with positive
length of the appropriate dimension of
x. The list can have
names. It is possible that all components are
dimnames may get converted to
"data.frame" method both dimnames are character
vectors, and the rownames must contain no duplicates nor missing
x, with “
dimnames, i.e. each component a character vector of
Setting components of the dimnames, e.g.,
dimnames(A)[] <- value is a common paradigm, but note that
it will not work if the value assigned is
rownames instead, or (as it does) manipulate the whole
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
## simple versions of rownames and colnames ## could be defined as follows rownames0 <- function(x) dimnames(x)[] colnames0 <- function(x) dimnames(x)[] (dn <- dimnames(A <- provideDimnames(N <- array(1:24, dim = 2:4)))) A0 <- A; dimnames(A)[2:3] <- list(NULL) stopifnot(identical(A0, provideDimnames(A))) strd <- function(x) utils::str(dimnames(x)) strd(provideDimnames(A, base= list(letters[-(1:9)], tail(LETTERS)))) strd(provideDimnames(N, base= list(letters[-(1:9)], tail(LETTERS)))) # recycling strd(provideDimnames(A, base= list(c("AA","BB")))) # recycling on both levels ## set "empty dimnames": provideDimnames(rbind(1, 2:3), base = list(""), unique=FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.