1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#' Tools for working with row names or row ids
#'
#' @param x A data frame.
#' @param var Name of column to use for row names/ids. For `column_as_rownames()`,
#' this argument can be the variable name or the column number. For
#' `rownames_as_column()` and `rowid_as_column()`, the column name must not
#' already exist in the data.
#'
#' @details
#' These are similar to `tibble`'s functions `column_to_rownames()`,
#' `rownames_to_column()` and `rowid_to_column()`. Note that the behavior of
#' `rowid_as_column()` is different for grouped dataframe: instead of making
#' the rowid unique across the full dataframe, it creates rowid per group.
#' Therefore, there can be several rows with the same rowid if they belong to
#' different groups.
#'
#' If you are familiar with `dplyr`, this is similar to doing the following:
#' ```r
#' data |>
#' group_by(grp) |>
#' mutate(id = row_number()) |>
#' ungroup()
#' ```
#'
#' @return
#' A data frame.
#'
#' @rdname rownames
#'
#' @examples
#' # Convert between row names and column --------------------------------
#' test <- rownames_as_column(mtcars, var = "car")
#' test
#' head(column_as_rownames(test, var = "car"))
#'
#' @export
rownames_as_column <- function(x, var = "rowname") {
if (!insight::object_has_rownames(x)) {
insight::format_error("The data frame doesn't have rownames.")
}
if (is.null(var)) {
var <- "rowname"
}
if (!is.character(var)) {
insight::format_error("Argument 'var' must be of type character.")
}
if (var %in% colnames(x)) {
insight::format_error(
paste0("There is already a variable named `", var, "` in your dataset.")
)
}
original_x <- x
rn <- data.frame(rn = rownames(x), stringsAsFactors = FALSE)
x <- cbind(rn, x)
colnames(x)[1] <- var
rownames(x) <- NULL
x <- .replace_attrs(x, attributes(original_x))
x
}
#' @rdname rownames
#' @export
column_as_rownames <- function(x, var = "rowname") {
if (!is.character(var) && !is.numeric(var)) {
insight::format_error("Argument `var` must be of type character or numeric.")
}
if (is.character(var) && !var %in% names(x)) {
insight::format_error(paste0("Variable \"", var, "\" is not in the data frame."))
}
if (is.numeric(var) && (var > ncol(x) || var <= 0)) {
insight::format_error("Column ", var, " does not exist. There are ", ncol(x), " columns in the data frame.")
}
original_x <- x
rownames(x) <- x[[var]]
x[[var]] <- NULL
x <- .replace_attrs(x, attributes(original_x))
x
}
#' @rdname rownames
#' @export
#' @examples
#' test_data <- head(iris)
#'
#' rowid_as_column(test_data)
#' rowid_as_column(test_data, var = "my_id")
rowid_as_column <- function(x, var = "rowid") {
UseMethod("rowid_as_column")
}
#' @export
rowid_as_column.default <- function(x, var = "rowid") {
if (is.null(var)) {
var <- "rowid"
}
if (!is.character(var)) {
insight::format_error("Argument 'var' must be of type character.")
}
if (var %in% colnames(x)) {
insight::format_error(
paste0("There is already a variable named `", var, "` in your dataset.")
)
}
original_x <- x
rn <- data.frame(rn = seq_len(nrow(x)), stringsAsFactors = FALSE)
x <- cbind(rn, x)
colnames(x)[1] <- var
rownames(x) <- NULL
x <- .replace_attrs(x, attributes(original_x))
x
}
#' @export
rowid_as_column.grouped_df <- function(x, var = "rowid") {
if (!is.character(var)) {
insight::format_error("Argument 'var' must be of type character.")
}
if (var %in% colnames(x)) {
insight::format_error(
paste0("There is already a variable named `", var, "` in your dataset.")
)
}
grps <- attr(x, "groups", exact = TRUE)
grps <- grps[[".rows"]]
for (i in seq_along(grps)) {
x[grps[[i]], var] <- seq_along(grps[[i]])
}
# can't just put select = "var" because there could be another variable
# called var
x <- data_relocate(x, paste0("^", var, "$"), regex = TRUE, before = 1)
x
}
|