Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

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_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().

Usage

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)
have_name(mtcars)
is_dictionaryish(mtcars)

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

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


# For empty objects the semantics are slightly different.
# is_dictionaryish() returns TRUE for empty objects:
is_dictionaryish(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_name() can show you
# where the problem is:
is_named(invalid)
have_name(invalid)

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

rlang

Functions for Base Types and Core R and 'Tidyverse' Features

v0.4.11
MIT + file LICENSE
Authors
Lionel Henry [aut, cre], Hadley Wickham [aut], mikefc [cph] (Hash implementation based on Mike's xxhashlite), Yann Collet [cph] (Author of the embedded xxHash library), RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.