is_named() checks that x has names attributes, and that none of the names are missing or empty (NA or ""). is_dictionaryish() checks that an object is a dictionary: that it has actual names and in addition that there are no duplicated names. have_name() is a vectorised version of is_named().

is_named(x)

is_dictionaryish(x)

have_name(x)

Arguments

x

An object to test.

Value

is_named() and is_dictionaryish() are scalar predicates and return TRUE or FALSE. have_name() is vectorised and returns a logical vector as long as the input.

Examples

# A data frame usually has valid, unique names is_named(mtcars)
#> [1] TRUE
have_name(mtcars)
#> [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
is_dictionaryish(mtcars)
#> [1] TRUE
# But data frames can also have duplicated columns: dups <- cbind(mtcars, cyl = seq_len(nrow(mtcars))) is_dictionaryish(dups)
#> [1] FALSE
# The names are still valid: is_named(dups)
#> [1] TRUE
have_name(dups)
#> [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
# For empty objects the semantics are slightly different. # is_dictionaryish() returns TRUE for empty objects: is_dictionaryish(list())
#> [1] TRUE
# But is_named() will only return TRUE if there is a names # attribute (a zero-length character vector in this case): x <- set_names(list(), character(0)) is_named(x)
#> [1] TRUE
# Empty and missing names are invalid: invalid <- dups names(invalid)[2] <- "" names(invalid)[5] <- NA # is_named() performs a global check while have_name() can show you # where the problem is: is_named(invalid)
#> [1] FALSE
have_name(invalid)
#> [1] TRUE FALSE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
# have_name() will work even with vectors that don't have a names # attribute: have_name(letters)
#> [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [25] FALSE FALSE