rownames | R Documentation |
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()
.
rn2col(data, name = ".rn")
col2rn(data, name = ".rn")
has_rn(data)
rm_rn(data)
set_rn(data, value)
add_rowid(data, name = ".rowid")
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 |
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.
rn2col()
: moves the row names of data
to an explicit column
whether they are explicit or implicit.
col2rn()
: is the inverse of rn2col()
. 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 at 1:nrow(data)
.
It does not remove existing row names currently, but may in the future
(please code accordingly).
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.