R/check_distinct_ids.R

Defines functions check_distinct_ids

#' Internal function which creates cross-tables with number of distinct id's
#' @inheritParams common_arguments
#' @importFrom data.table uniqueN
#' @noRd
check_distinct_ids <- function(data, id_var, val_var = NULL, by = NULL) {
  distinct_ids <- NULL # to silence NSE notes in RCMD check

  # handle the case where no val_var is provided
  if (is.null(val_var)) {
    val_var <- id_var
  }

  distinct_ids <- data[
    i = !is.na(get(id_var)) & !is.na(get(val_var)),
    j = list(distinct_ids = data.table::uniqueN(get(id_var))),
    keyby = by
  ][
    order(distinct_ids)
  ]

  structure(
    distinct_ids,
    class = c("sdc_distinct_ids", "data.table", "data.frame")
  )
}

Try the sdcLog package in your browser

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

sdcLog documentation built on March 20, 2022, 1:06 a.m.