rownames {SomaDataIO}R Documentation

Helpers for Working With Row Names

Description

Easily move row names to a column and vice-versa without the unwanted side-effects to object class and attributes. Drop-in replacement for tibble::rownames_to_column() and tibble::column_to_rownames() which can have undesired side-effects to complex object attributes. Does not import any external packages, modify the environment, or change the object (other than the desired column). When using col2rn(), if explicit row names exist, they are overwritten with a warning. add_rowid() does not affect row names, which differs from tibble::rowid_to_column().

Usage

rn2col(data, name = ".rn")

col2rn(data, name = ".rn")

has_rn(data)

rm_rn(data)

set_rn(data, value)

add_rowid(data, name = ".rowid")

Arguments

data

An object that inherits from class data.frame. Typically a soma_adat class object.

name

Character. The name of the column to move.

value

Character. The new set of names for the data frame. If duplicates exist they are modified on-the-fly via make.unique().

Value

All functions attempt to return an object of the same class as the input with fully intact and unmodified attributes (aside from those required by the desired action). has_rn() returns a scalar logical.

Functions

Examples

df <- data.frame(a = 1:5, b = rnorm(5), row.names = LETTERS[1:5])
df
rn2col(df)              # default name is `.rn`
rn2col(df, "AptName")   # pass `name =`

# moving columns
df$mtcars <- sample(names(mtcars), 5)
col2rn(df, "mtcars")   # with a warning

# Move back and forth easily
# Leaves original object un-modified
identical(df, col2rn(rn2col(df)))

# add "id" column
add_rowid(mtcars)

# remove row names
has_rn(mtcars)
mtcars2 <- rm_rn(mtcars)
has_rn(mtcars2)

[Package SomaDataIO version 6.1.0 Index]