Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement name_rows to save and restore row names.
Fixes #61
- Loading branch information
Showing
5 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,33 @@ | |||
#' Toggle row names between explicit and implicit. | |||
#' | |||
#' Plyr functions ignore row names, so this function provides a way to preserve | |||
#' them by converting them to an explicit column in the data frame. After the | |||
#' plyr operation, you can then apply \code{name_rows} again to convert back | |||
#' from the explicit column to the implicit \code{rownames}. | |||
#' | |||
#' @param df a data.frame, with either \code{rownames}, or a column called | |||
#' \code{.rownames}. | |||
#' @export | |||
#' @examples | |||
#' name_rows(mtcars) | |||
#' name_rows(name_rows(mtcars)) | |||
#' | |||
#' df <- data.frame(a = sample(10)) | |||
#' arrange(df, a) | |||
#' arrange(name_rows(df), a) | |||
#' name_rows(arrange(name_rows(df), a)) | |||
name_rows <- function(df) { | |||
stopifnot(is.data.frame(df)) | |||
|
|||
rn_col <- !is.null(df$.rownames) | |||
|
|||
if (rn_col) { | |||
rownames(df) <- df$.rownames | |||
df$.rownames <- NULL | |||
} else { | |||
df$.rownames <- rownames(df) | |||
rownames(df) <- NULL | |||
} | |||
|
|||
df | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,28 @@ | |||
\name{name_rows} | |||
\alias{name_rows} | |||
\title{Toggle row names between explicit and implicit.} | |||
\usage{ | |||
name_rows(df) | |||
} | |||
\arguments{ | |||
\item{df}{a data.frame, with either \code{rownames}, or a | |||
column called \code{.rownames}.} | |||
} | |||
\description{ | |||
Plyr functions ignore row names, so this function | |||
provides a way to preserve them by converting them to an | |||
explicit column in the data frame. After the plyr | |||
operation, you can then apply \code{name_rows} again to | |||
convert back from the explicit column to the implicit | |||
\code{rownames}. | |||
} | |||
\examples{ | |||
name_rows(mtcars) | |||
name_rows(name_rows(mtcars)) | |||
|
|||
df <- data.frame(a = sample(10)) | |||
arrange(df, a) | |||
arrange(name_rows(df), a) | |||
name_rows(arrange(name_rows(df), a)) | |||
} | |||
|