R/process_from_to_data.R

Defines functions process_from_to_data

process_from_to_data <- function(data, from, to) {

  if (!is.data.frame(data)) {
    abort("'data' must be a data.frame.")
  }
  if (!is_string(from) && !is_scalar_integerish(from)) {
    abort("'from' must be a string or integer.")
  }
  if (!is_string(to) && !is_scalar_integerish(to)) {
    abort("'to' must be a string or integer.")
  }
  if (is.numeric(from)) {
    if (from <= 0 || from > ncol(data)) {
      abort("If 'from' is supplied as an integer, it must be the index of the \"from\" column in the dataset.")
    }
    from <- names(data)[from]
  }
  if (is.numeric(to)) {
    if (to <= 0 || to > ncol(data)) {
      abort("If 'to' is supplied as an integer, it must be the index of the \"to\" column in the dataset.")
    }
    to <- names(data)[to]
  }

  if (!all(c(from, to) %in% names(data))) {
    if (from == "from" && to == "to") {
      abort("The supplied dataset must have a \"from\" column and a \"to\" column.")
    }
    else {
      abort("The values supplied to 'from' and 'to' must be names or indices of variables in the supplied dataset.")
    }
  }

  if (identical(from, to)) {
    abort("'from' and 'to' cannot be the same column.")
  }

  #Reorder so from and to are at beginning
  from_to_ind <- match(c(from, to), names(data))
  not_from_to_ind <- seq_len(ncol(data))[-from_to_ind]
  data <- data[c(from_to_ind, not_from_to_ind)]

  if (anyNA(data[[1]]) || anyNA(data[[2]])) {
    data_clean <- data[!is.na(data[[1]]) & !is.na(data[[2]]),, drop = FALSE]
    warn(paste("Dropping", nrow(data) - nrow(data_clean), "rows missing 'from' or 'to' values."))

    data <- data_clean
  }

  attr(data, "from") <- from
  attr(data, "to") <- to

  return(data)
}
judgelord/netlit documentation built on Jan. 3, 2023, 2:42 p.m.