Skip to content

Commit

Permalink
Implement add_rownames()
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Nov 20, 2014
1 parent 62469f7 commit 8a556c0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ S3method(update,tbl_sql)
export("%.%")
export("%>%")
export(.datatable.aware)
export(add_rownames)
export(anti_join)
export(arrange)
export(arrange_)
Expand Down
19 changes: 19 additions & 0 deletions R/dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,22 @@ as_data_frame <- function(x) {

x
}

#' Convert row names to an explicit variable.
#'
#' @param df Input data frame with rownames.
#' @param var Name of variable to use
#' @export
#' @examples
#' mtcars %>%
#' tbl_df() %>%
#' print() %>%
#' add_rownames()
add_rownames <- function(df, var = "rowname") {
stopifnot(is.data.frame(df))

df[[var]] <- rownames(df)
rownames(df) <- NULL

df
}
22 changes: 22 additions & 0 deletions man/add_rownames.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
% Generated by roxygen2 (4.0.2): do not edit by hand
\name{add_rownames}
\alias{add_rownames}
\title{Convert row names to an explicit variable.}
\usage{
add_rownames(df, var = "rowname")
}
\arguments{
\item{df}{Input data frame with rownames.}

\item{var}{Name of variable to use}
}
\description{
Convert row names to an explicit variable.
}
\examples{
mtcars \%>\%
tbl_df() \%>\%
print() \%>\%
add_rownames()
}

0 comments on commit 8a556c0

Please sign in to comment.