R/isfield.R

Defines functions isfield

Documented in isfield

#' @title Checks if a list contains a field
#' @description This function tries to replicate the behavior of the `isfield`
#' function in Matlab
#' @param x list
#' @param field name of field
#' @references https://se.mathworks.com/help/matlab/ref/isfield.html
#' @return A logical vector determining if \code{field} is within
#' \code{names(x)}
#' @export
#' @examples
#' S <- list(
#'   x = rnorm(100),
#'   title = "x"
#' )
#' isfield(S, "title")
#' isfield(S, "z")
isfield <- function(x, field) {
  vapply(
    X = field,
    FUN = function(f) f %in% names(x),
    FUN.VALUE = vector("logical", 1)
  )
}

Try the matlab2r package in your browser

Any scripts or data that you put into this service are public.

matlab2r documentation built on Feb. 16, 2023, 10:39 p.m.