rownames: Tools for working with row names or row ids

rownames_as_columnR Documentation

Tools for working with row names or row ids

Description

Tools for working with row names or row ids

Usage

rownames_as_column(x, var = "rowname")

column_as_rownames(x, var = "rowname")

rowid_as_column(x, var = "rowid")

Arguments

x

A data frame.

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:

data |>
  group_by(grp) |>
  mutate(id = row_number()) |>
  ungroup()

Value

A data frame.

Examples

# Convert between row names and column --------------------------------
test <- rownames_as_column(mtcars, var = "car")
test
head(column_as_rownames(test, var = "car"))

test_data <- head(iris)

rowid_as_column(test_data)
rowid_as_column(test_data, var = "my_id")

datawizard documentation built on Oct. 6, 2024, 1:08 a.m.