-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/0.3-2' into production
- Loading branch information
Showing
9 changed files
with
149 additions
and
91 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
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#' Row names | ||
#' | ||
#' \code{has_rownames} checks if a data frame has row names. | ||
#' @param df Input data frame | ||
#' @export | ||
#' @rdname rownames | ||
#' @examples | ||
#' has_rownames(mtcars) | ||
#' has_rownames(iris) | ||
has_rownames <- function(df) { | ||
stopifnot(is.data.frame(df)) | ||
.row_names_info(df) > 0L | ||
} | ||
|
||
#' \code{remove_rownames} removes all row names. | ||
#' @export | ||
#' @rdname rownames | ||
#' @examples | ||
#' rownames(remove_rownames(mtcars)) | ||
remove_rownames <- function(df) { | ||
stopifnot(is.data.frame(df)) | ||
rownames(df) <- NULL | ||
df | ||
} | ||
|
||
#' \code{rownames_to_column} convert row names to an explicit variable. | ||
#' | ||
#' @param var Name of variable to use | ||
#' @export | ||
#' @rdname rownames | ||
#' @importFrom stats setNames | ||
#' @examples | ||
#' rownames_to_column(mtcars) | ||
#' | ||
#' mtcars_tbl <- rownames_to_column(tbl_df(mtcars)) | ||
#' mtcars_tbl | ||
rownames_to_column <- function(df, var = "rowname") { | ||
stopifnot(is.data.frame(df)) | ||
|
||
if (var %in% colnames(df)) | ||
stop("There is a column named ", var, " already!", call. = FALSE) | ||
|
||
rn <- data_frame(rownames(df)) | ||
names(rn) <- var | ||
|
||
attribs <- attributes(df) | ||
|
||
new_df <- c(rn, df) | ||
attribs[["names"]] <- names(new_df) | ||
|
||
attributes(new_df) <- attribs[names(attribs) != "row.names"] | ||
attr(new_df, "row.names") <- .set_row_names(nrow(df)) | ||
new_df | ||
} | ||
|
||
#' \code{column_to_rownames} convert a column variable to row names. This is an | ||
#' inverted operation of \code{rownames_to_column}. | ||
#' | ||
#' @rdname rownames | ||
#' @export | ||
#' @examples | ||
#' | ||
#' column_to_rownames(mtcars_tbl) | ||
column_to_rownames <- function(df, var = "rowname") { | ||
stopifnot(is.data.frame(df)) | ||
|
||
if (has_rownames(df)) | ||
stop("This data frame already has row names.", call. = FALSE) | ||
|
||
if (!var %in% colnames(df)) | ||
stop("This data frame has no column named ", var, ".", call. = FALSE) | ||
|
||
rownames(df) <- df[[var]] | ||
df[[var]] <- 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
context("rownames") | ||
|
||
test_that("has_rownames and remove_rownames", { | ||
expect_false(has_rownames(iris)) | ||
expect_true(has_rownames(mtcars)) | ||
expect_false(has_rownames(remove_rownames(mtcars))) | ||
expect_false(has_rownames(remove_rownames(iris))) | ||
expect_error(has_rownames(1:10)) | ||
}) | ||
|
||
test_that("rownames_to_column keeps the tbl classes (#882)", { | ||
res <- rownames_to_column( mtcars, "Make&Model" ) | ||
expect_false(has_rownames(res)) | ||
expect_equal( class(res), class(mtcars) ) | ||
expect_error(rownames_to_column( mtcars, "wt"), | ||
paste("There is a column named wt already!") ) | ||
res1 <- rownames_to_column( tbl_df(mtcars), "Make&Model" ) | ||
expect_false(has_rownames(res1)) | ||
expect_equal( class(res1), class(tbl_df(mtcars)) ) | ||
expect_error(rownames_to_column( mtcars, "wt"), | ||
paste("There is a column named wt already!") ) | ||
}) | ||
|
||
test_that("column_to_rownames returns tbl", { | ||
var <- "car" | ||
mtcars <- tbl_df(mtcars) | ||
res <- column_to_rownames( rownames_to_column( mtcars, var), var) | ||
expect_true(has_rownames(res)) | ||
expect_equal( class(res), class(mtcars) ) | ||
expect_equal(rownames(res), rownames(mtcars)) | ||
expect_equal(res, mtcars) | ||
expect_false(var %in% names(res)) | ||
|
||
mtcars$num <- rev(seq_len(nrow(mtcars))) | ||
res1 <- column_to_rownames( rownames_to_column( mtcars), var="num") | ||
expect_true(has_rownames(res1)) | ||
expect_equal(rownames(res1), as.character(mtcars$num) ) | ||
expect_error(column_to_rownames(res1), "This data frame already has row names.") | ||
expect_error(column_to_rownames( rownames_to_column( mtcars, var), "num2"), | ||
paste("This data frame has no column named num2.") ) | ||
}) |