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 |
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 |
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
-
rn2col()
: moves the row names ofdata
to an explicit column whether they are explicit or implicit. -
col2rn()
: is the inverse ofrn2col()
. If row names exist, they will be overwritten (with warning). -
has_rn()
: returns a boolean indicating whether the data frame has explicit row names assigned. -
rm_rn()
: removes existing row names, leaving only "implicit" row names. -
set_rn()
: sets (and overwrites) existing row names for data frames only. -
add_rowid()
: adds a sequential integer row identifier; starting at1:nrow(data)
. It does not remove existing row names currently, but may in the future (please code accordingly).
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)