Learn R Programming

assertr (version 3.0.1)

has_only_names: Returns TRUE if data.frame or list has only the specified names

Description

This function checks parent frame environment for a specific set of names; if more columns are present than those specified, an error is raised.

Usage

has_only_names(...)

Value

TRUE is all names exist, FALSE if not

Arguments

...

A arbitrary amount of quoted names to check for

Details

This is meant to be used with `assertr`'s `verify` function to check for the existence of specific column names in a `data.frame` that is piped to `verify`. It can also work on a non-`data.frame` list.

See Also

Other Name verification: has_all_names()

Examples

Run this code

# The last two columns names are switched in order, but all column names are
# present, so it passes.
verify(
  mtcars,
  has_only_names(c(
    "mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am",
    "carb", "gear"
  ))
)

# More than one set of character strings can be provided.
verify(
  mtcars,
  has_only_names(
    c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am"),
    c("carb", "gear")
  )
)

if (FALSE) {
# The some columns are missing, so it fails.
verify(mtcars, has_only_names("mpg"))
}

Run the code above in your browser using DataLab