R/subset.mitml.list.R

Defines functions subset.mitml.list

Documented in subset.mitml.list

subset.mitml.list <- function(x, subset, select, ...){
# subset list of multiply imputed data sets
# NOTE: code adapted from subset.data.frame (by Peter Dalgaard and Brian Ripley)

  rind <- if (missing(subset)) {
    lapply(x, function(i) rep(TRUE, nrow(i)))
  } else {
    ss <- substitute(subset)
    rind <- lapply(x, function(i) eval(ss, i, parent.frame()))
    if (!is.logical(unlist(rind))) stop("'subset' must be logical")
    lapply(rind, function(i) i & !is.na(i))
  }

  cind <- if (missing(select)) {
    lapply(x, function(i) TRUE)
  } else {
    nl <- lapply(x, function(i){
      l <- as.list(seq_along(i))
      names(l) <- names(i)
      l
    })
    se <- substitute(select)
    lapply(nl, function(i) eval(se, i, parent.frame()))
  }

  res <- lapply(seq_along(x), function(i) x[[i]][rind[[i]], cind[[i]], drop = FALSE])
  as.mitml.list(res)
  
}

Try the mitml package in your browser

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

mitml documentation built on Jan. 6, 2023, 5:17 p.m.