Skip to content

Instantly share code, notes, and snippets.

@mkearney
Last active October 20, 2016 16:32
Show Gist options
  • Save mkearney/c2a95f03790c6e2a67a7df5fb84b1436 to your computer and use it in GitHub Desktop.
Save mkearney/c2a95f03790c6e2a67a7df5fb84b1436 to your computer and use it in GitHub Desktop.
#' xiny
#'
#' Returns logical value indicating whether named
#' object includes var name. Functions do the following:
#'
#' \itemize{
#' \item \code{\%xy\%} returns logical for each value of x
#' \item \code{\%any\%} returns TRUE if y contains any of x
#' \item \code{\%all\%} returns TRUE if y contains all of x
#' }
#' @name xiny
#' @param x Character, name of variable of interest.
#' @param y Named object on which to search for \code{x}.
#'
#' @examples
#' # mpg in mtcars
#' "mpg" %xy% mtcars
#'
#' # year not in mtcars
#' "year" %xy% mtcars
#'
#' # check each name
#' c("mpg", "year") %xy% mtcars
#'
#' # check for any
#' c("mpg", "year") %any% mtcars
#'
#' # check for all
#' c("mpg", "year") %all% mtcars
#'
#' @return Logical vector of length \code{length(x)}.
#' @export
`%xy%` <- function(x, y) {
if (is.null(names(x))) {
var <- x
dat <- y
} else {
dat <- x
var <- y
}
var %in% names(dat)
}
#' xanyy
#'
#' Returns logical value indicating whether named
#' object includes var name. Functions do the following:
#'
#' \itemize{
#' \item \code{\%xy\%} returns logical for each value of x
#' \item \code{\%any\%} returns TRUE if y contains any of x
#' \item \code{\%all\%} returns TRUE if y contains all of x
#' }
#' @name xanyy
#' @param x Character, name of variable of interest.
#' @param y Named object on which to search for \code{x}.
#' @export
`%any%` <- function(x, y) {
if (is.null(names(x))) {
var <- x
dat <- y
} else {
dat <- x
var <- y
}
any(var %in% names(dat))
}
#' xally
#'
#' Returns logical value indicating whether named
#' object includes var name. Functions do the following:
#'
#' \itemize{
#' \item \code{\%xy\%} returns logical for each value of x
#' \item \code{\%any\%} returns TRUE if y contains any of x
#' \item \code{\%all\%} returns TRUE if y contains all of x
#' }
#' @name xally
#' @param x Character, name of variable of interest.
#' @param y Named object on which to search for \code{x}.
#' @export
#' @export
`%all%` <- function(x, y) {
if (is.null(names(x))) {
var <- x
dat <- y
} else {
dat <- x
var <- y
}
all(var %in% names(dat))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment