hasName

0th

Percentile

Check for Name

hasName is a convenient way to test for one or more names in an R object.

Keywords
manip, logic
Usage
hasName(x, name)
Arguments
x

Any object.

name

One or more character values to look for.

Details

hasName(x, name) is defined to be equivalent to name %in% names(x), though it will evaluate slightly more quickly. It is intended to replace the common idiom !is.null(x$name). The latter can be unreliable due to partial name matching; see the example below.

Value

A logical vector of the same length as name containing TRUE if the corresponding entry is in names(x).

See Also

%in%, exists

Aliases
  • hasName
Examples
library(utils) # NOT RUN { x <- list(abc = 1, def = 2) !is.null(x$abc) # correct !is.null(x$a) # this is the wrong test! hasName(x, "abc") hasName(x, "a") # }
Documentation reproduced from package utils, version 3.6.1, License: Part of R 3.6.1

Community examples

Looks like there are no examples yet.