R/assign.R

# DATA CLEANING: ASSIGN
#' @include AllGenerics.R
NULL

#' @export
#' @rdname assign
#' @aliases assign_rownames,data.frame-method
setMethod(
  f = "assign_rownames",
  signature = c(x = "data.frame"),
  definition = function(x, column, remove = TRUE) {
    ## Validation
    assert_length(column, 1)
    if ((column > ncol(x)) | (column < 1)) return(x)

    y <- x
    rownames(y) <- y[, column]
    if (remove) {
      y <- y[, -column, drop = FALSE]
    }
    y
  }
)

#' @export
#' @rdname assign
#' @aliases assign_colnames,data.frame-method
setMethod(
  f = "assign_colnames",
  signature = c(x = "data.frame"),
  definition = function(x, row, remove = TRUE) {
    ## Validation
    assert_length(row, 1)
    if ((row > nrow(x)) | (row < 1)) return(x)

    y <- x
    colnames(y) <- y[row, ]
    if (remove) {
      y <- y[-row, , drop = FALSE]
    }
    y
  }
)

Try the arkhe package in your browser

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

arkhe documentation built on Nov. 17, 2023, 5:09 p.m.