rownames: Tools for working with row names

Description Usage Arguments Value Examples

Description

In some quarters, it is considered best to avoid row names, because they are effectively a character column with different semantics than every other column. These functions allow to you detect if a data.frame has row names (has_rownames()), remove them (remove_rownames()), or convert them back-and-forth between an explicit column (rownames_to_column() and column_to_rownames()). Also included is rowid_to_column(), which adds a column at the start of the dataframe of ascending sequential row ids starting at 1. Note that this will remove any existing row names.

Usage

1
2
3
4
5
6
7
8
9
rownames_to_column(.data, var = "rowname")

rowid_to_column(.data, var = "rowid")

column_to_rownames(.data, var = "rowname")

remove_rownames(.data)

has_rownames(.data)

Arguments

.data

A data.frame.

var

character(1). The name of the column to use for row names.

Value

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Detect row names
has_rownames(mtcars)
has_rownames(iris)

# Remove row names
remove_rownames(mtcars) %>% has_rownames()

# Convert between row names and column
mtcars <- rownames_to_column(mtcars, var = "car")
column_to_rownames(mtcars, var = "car") %>% head()

# Adding rowid as a column
rowid_to_column(iris) %>% head()

poorman documentation built on March 28, 2021, 5:05 p.m.