R/row_seq.R

Defines functions row_seq

Documented in row_seq

#' Append Row Number as a Variable to a Dataframe
#'
#' This function takes a dataframe and adds a variable of row numbers.
#' This is the simplest function that does not take into account any group
#' structures.
#'
#' @param df Input dataframe.
#' @param row Name for the row variable.
#'
#' @export

row_seq <- function(df, row = "row") {
  if (length(row) != 1) {
    stop("Invalid input for argument row.")
  }
  if (row %in% names(df)) {
    stop("Set another name for the row variable to be created.")
  }
  if (nrow(df) > 0) {
    df[row] <- seq(nrow(df))
  }
  return(df)
}
sysilviakim/Kmisc documentation built on Jan. 28, 2023, 10:58 a.m.