rlang (version 0.0.0.9000)

is_named: Is object named?

Description

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

Usage

is_named(x)
is_dictionary(x)
have_names(x)

Arguments

x
An object to test.

Value

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

Examples

Run this code
# A data frame usually has valid, unique names
is_named(mtcars)
have_names(mtcars)
is_dictionary(mtcars)

# But data frames can also have duplicated columns:
dups <- cbind(mtcars, cyl = seq_len(nrow(mtcars)))
is_dictionary(dups)

# The names are still valid:
is_named(dups)
have_names(dups)


# For empty objects the semantics are slightly different.
# is_dictionary() returns TRUE for empty objects:
is_dictionary(list())

# 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)


# Empty and missing names are invalid:
invalid <- dups
names(invalid)[2] <- ""
names(invalid)[5] <- NA

# is_named() performs a global check while have_names() can show you
# where the problem is:
is_named(invalid)
have_names(invalid)

# have_names() will work even with vectors that don't have a names
# attribute:
have_names(letters)

Run the code above in your browser using DataCamp Workspace