Attribute checks - Dimensions existence and sizes
attributes-dimensions.RdFunctions for the presence and number of dimensions and dimension names of an object.
n_dims()andn_dimnames()return the number of dimensions and dimension names, respectively, controlling for empty dimensions and invalid names.has_dim(),has_dimnames(), andhas_rownames()return whether the object has dimensions, dimension names, and rownames, respectively.
Usage
n_dims(x, empty = TRUE, how = "dim")
n_dimnames(x, invalid = TRUE, how = "dimnames")
has_dimnames(x, n = NULL, how = "dimnames")
has_rownames(x, how = "dimnames")
has_dim(x, n = NULL, how = "dim")Arguments
- x
[
any] An object to test.- empty
[
TRUE|FALSE] Whether to count empty dimensions and dimension names.- how
[
character(1)] How to extract the attribute:For dimensions:
"dim"fordim()and"attr"forattr(x, "dim").For dimension names:
"dimnames"fordimnames()and"attr"forattr(x, "dimnames").For rownames:
"rownames"forrownames(),"dimnames"for the first element ofdimnames(), and"attr"forattr(x, "row.names").
- invalid
[
TRUE|FALSE] Whether to count dimensions with NA, empty, or duplicate names.- n
[
integer(1)] Number of dimensions to check for. Set toNULLto not test.
Value
[integer(1)] For
n_dims()andn_dimnames().[
TRUE|FALSE] Forhas_dim(),has_dimnames(), andhas_rownames(): the scalar result of the test.
Examples
x <- structure(
1:6, dim = c(2, 3, 1),
dimnames = list(c("a", "a"), c("x", "y", "z"), NULL)
)
# Counting dimensions and dimension names:
n_dims(x) #> 3
#> [1] 3
n_dims(x, empty = FALSE) #> 2
#> [1] 2
n_dimnames(x) #> 2
#> [1] 2
n_dimnames(x, invalid = FALSE) #> 1 (rownames have duplicates)
#> [1] 1
# Testing for dimensions and dimension names:
has_dimnames(x) #> TRUE
#> [1] TRUE
has_rownames(mtcars) #> TRUE
#> [1] TRUE
has_dim(mtcars) #> TRUE
#> [1] TRUE
# Data frames often have no actual dimension attribues:
has_dim(mtcars, how = "attr") #> FALSE
#> [1] FALSE
has_dimnames(mtcars, how = "attr") #> FALSE
#> [1] FALSE
has_rownames(mtcars, how = "row.names") #> TRUE (but have row.names one)
#> [1] TRUE