decorateDims: Add dimension names to a matrix or array with (partially)...

Description Usage Arguments Value Note See Also Examples

View source: R/array_transform.R

Description

decorateDims is not unlike provideDimnames: it provides dimension names for dimensions which have 'missing' dimension names ('missing' in a broad sense: either NA, NULL or ""). The main difference is that it has a more convenient interface for providing names of dimensions, and via decorateDims_ the user can modify the input data in place (without creating even a shallow copy).

Usage

1
2
3
decorateDims(dat, .names = TRUE, .dimnames = TRUE)

decorateDims_(dat, .names = TRUE, .dimnames = TRUE)

Arguments

dat

a matrix or array

.names

either 1) a logical value whether dimension identifiers (names of dimnames) should be checked and corrected (default: TRUE), or 2) a character vector of new dimension identifiers for missing, NA or "" dimension identifiers, or 3) a single character value after which 1, 2, ... is appended to create unique identifiers (see Examples)

.dimnames

either 1) a logical value whether dimnames should be checked and corrected (default: TRUE), or 2) a list of character vectors of new dimnames for dimensions with missing, NA or "" dimnames, or 3) a single character value after which 1, 2, ... is appended to create unique dimension levels (see Examples). If '.names' is not explicitly provided but '.dimnames' is a named list, its names are considered as '.names'.

Value

decorateDims returns a copy of the original matrix or array with non-null dimension names. decorateDims invisibly returns the original matrix or array with modified dimnames attribute.

Note

Use decorateDims_ with extra care because it modifies in place all objects which 'dat' refers to.

See Also

provideDimnames for a slightly different solution

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# create a matrix without dimension names
( mat <- matrix(letters[1:8], 2, 4) )

# add default dimension names
( mat2 <- decorateDims(mat) )

# remove the second dimension identifier (name of the 'dimnames' attribute)
names(dimnames(mat2))[2] <- ""
mat2

# create a new variable which is referenced to 'mat2'
mat3 <- mat2

# modify the names of dimnames in place
decorateDims_(mat3, .names = "Dimension")
mat3
stopifnot(identical(names(dimnames(mat3)),
                    c("_Dim1", "Dimension2")))

# NOTE that the attributes of 'mat2' has been also changed!
mat2
stopifnot(identical(dimnames(mat2), dimnames(mat3)))

# decorate only with dimension identifiers, but no dimension names
( mat4 <- decorateDims(mat,
                       .names = c("rows", "columns"),
                       .dimnames = FALSE) )
stopifnot(identical(dimnames(mat4), list(rows = NULL, columns = NULL)))

tdeenes/eegR documentation built on April 19, 2021, 4:17 p.m.