R/vectorize.R

Defines functions vectorize

vectorize <- function(list, .class = NULL) {
  if (!is.null(.class)) {
    list <- lapply(list, `class<-`, .class)
  }

  vectorizable <- all(
    vapply(list, function(x) length(x) == 1 && rlang::is_atomic(x), logical(1))
  )

  if (!vectorizable) {
    return(list)
  }

  classes <- lapply(list, class)

  vector <- unlist(list)

  if (length(unique(classes)) == 1) {
    class(vector) <- unlist(unique(classes))
  }

  vector
}

Try the crossmap package in your browser

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

crossmap documentation built on Jan. 13, 2023, 1:13 a.m.