Wrappers around rlang predicates that allow multiple objects to be passed. The following documentation is adapted from the rlang documentation:
are_named()is a scalar predicate that checks that objects in...have anamesattribute and that none of the names are missing or empty (NAor"").are_named2()is likeare_named()but always returnsTRUEfor empty vectors, even those that don't have anamesattribute. In other words, it tests for the property that each element of a vector is named.are_named2()composes well withnames2()whereasare_named()composes withnames().have_names()is a vectorised variant.
Value
are_named() and are_named2() return a named logical,
or unnamed boolean if .all is TRUE. have_names() is vectorised
and returns a list of logical vectors whhere each is as long as the
input object. When .all is TRUE for have_names(), all logical
vectors are collapsed and a boolean is returned.
Examples
# are_named() is a scalar predicate about the whole vector of names:
x <- c(a = 1, b = 2)
are_named(x, c(a = 1, 2))
#> x c(a = 1, 2)
#> TRUE FALSE
are_named(x, c(a = 1, 2), .all = TRUE)
#> [1] FALSE
# Unlike are_named2(), are_named() returns `FALSE` for empty vectors
# that don't have a `names` attribute.
are_named(list(), vector())
#> list() vector()
#> FALSE FALSE
are_named2(list(), vector())
#> list() vector()
#> TRUE TRUE
# have_names() is vectorised
y <- c(a = 1, 2)
have_names(x, y, c(a = 1, 2, 3))
#> $x
#> [1] TRUE TRUE
#>
#> $y
#> [1] TRUE FALSE
#>
#> $`c(a = 1, 2, 3)`
#> [1] TRUE FALSE FALSE
#>
have_names(x, y, c(a = 1, 2, 3), .all = TRUE)
#> [1] FALSE
# Empty and missing names are treated as invalid:
invalid <- setNames(letters[1:5], letters[1:5])
names(invalid)[1] <- ""
names(invalid)[3] <- NA
are_named(invalid)
#> invalid
#> FALSE
have_names(invalid)
#> $invalid
#> [1] FALSE TRUE FALSE TRUE TRUE
#>
# A data frame normally has valid, unique names
# but a matrix usually doesn't because the names
# are stored in a different attribute.
mat <- matrix(1:4, 2)
colnames(mat) <- c("a", "b")
are_named(mtcars, mat)
#> mtcars mat
#> TRUE FALSE
have_names(mtcars, mat)
#> $mtcars
#> [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#>
#> $mat
#> [1] FALSE FALSE FALSE FALSE
#>
