Skip to content

Commit

Permalink
new rownames arg, closes #288, closes #289
Browse files Browse the repository at this point in the history
- `as_tibble()` gains `rownames` argument (#288, #289).
  • Loading branch information
krlmlr committed Oct 4, 2017
1 parent eb09971 commit 699dd9a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
21 changes: 16 additions & 5 deletions R/as_tibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#' want to suppress this when you know that you already have a valid data
#' frame and you want to save some time, or to explicitly enable it
#' if you have a tibble that you want to re-check.
#' @param rownames If `NULL`, remove row names (default for matrices, may become
#' default for data frames in the future). If `NA`, keep row names (current
#' default for data frames). Otherwise, the name of the new column that will
#' contain the existing row names.
#' @export
#' @examples
#' l <- list(x = 1:500, y = runif(500), z = 500:1)
Expand Down Expand Up @@ -57,15 +61,22 @@ as_tibble <- function(x, ...) {

#' @export
#' @rdname as_tibble
as_tibble.tbl_df <- function(x, ..., validate = FALSE) {
as_tibble.tbl_df <- function(x, ..., validate = FALSE, rownames = NULL) {
if (validate) return(NextMethod())
x
}

#' @export
#' @rdname as_tibble
as_tibble.data.frame <- function(x, validate = TRUE, ...) {
list_to_tibble(x, validate, raw_rownames(x))
as_tibble.data.frame <- function(x, validate = TRUE, ..., rownames = NA) {
result <- list_to_tibble(x, validate, raw_rownames(x))
if (is.null(rownames)) {
remove_rownames(result)
} else if (is.na(rownames)) {
result
} else {
rownames_to_column(result, var = rownames)
}
}

#' @export
Expand Down Expand Up @@ -99,8 +110,8 @@ list_to_tibble <- function(x, validate, rownames = NULL) {

#' @export
#' @rdname as_tibble
as_tibble.matrix <- function(x, ...) {
matrixToDataFrame(x)
as_tibble.matrix <- function(x, ..., rownames = NULL) {
as_tibble(matrixToDataFrame(x), ..., rownames = rownames)
}

#' @export
Expand Down
11 changes: 8 additions & 3 deletions man/as_tibble.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions src/matrixToDataFrame.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,8 @@ static SEXP get_class(void)
{
SEXP cls;

PROTECT(cls = Rf_allocVector(STRSXP, 3));
SET_STRING_ELT(cls, 0, Rf_mkChar("tbl_df"));
SET_STRING_ELT(cls, 1, Rf_mkChar("tbl"));
SET_STRING_ELT(cls, 2, Rf_mkChar("data.frame"));
PROTECT(cls = Rf_allocVector(STRSXP, 1));
SET_STRING_ELT(cls, 0, Rf_mkChar("data.frame"));
UNPROTECT(1);

return cls;
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-data-frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ test_that("as_tibble() can validate (#278)", {
})


test_that("as_tibble() can convert row names", {
df <- data.frame(a = 1:3, b = 2:4, row.names = letters[5:7])
expect_identical(
as_tibble(df, rownames = NULL),
tibble(a = 1:3, b = 2:4)
)
expect_identical(
as_tibble(df, rownames = "id"),
tibble(id = letters[5:7], a = 1:3, b = 2:4)
)
expect_identical(rownames(as_tibble(df)), rownames(df))
expect_identical(unclass(as_tibble(df)), unclass(df))
})


test_that("as_data_frame is an alias of as_tibble", {
expect_identical(as_data_frame(NULL), as_tibble(NULL))
})
Expand Down
29 changes: 22 additions & 7 deletions tests/testthat/test-matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,26 @@ test_that("forwarding to as.data.frame() for ts objects (#184)", {
})


test_that("converting from matrix keeps row names if argument has them", {
x <- matrix(1:30, 6, 5, dimnames = list(letters[1:6], LETTERS[1:5]))
df <- data.frame(A = 1:6, B = 7:12, C = 13:18, D = 19:24, E = 25:30,
row.names = letters[1:6])
out <- as_tibble(x)
expect_identical(rownames(out), rownames(x))
expect_identical(out, as_tibble(df))
test_that("converting from matrix removes row names by default", {
x <- matrix(1:30, 6, 5, dimnames = list(letters[1:6], LETTERS[1:5]))
df <- data.frame(A = 1:6, B = 7:12, C = 13:18, D = 19:24, E = 25:30)
out <- as_tibble(x)
expect_false(has_rownames(out))
expect_identical(out, as_tibble(df))
})

test_that("converting from matrix keeps row names if argument has them, with rownames = NA", {
x <- matrix(1:30, 6, 5, dimnames = list(letters[1:6], LETTERS[1:5]))
df <- data.frame(A = 1:6, B = 7:12, C = 13:18, D = 19:24, E = 25:30,
row.names = letters[1:6])
out <- as_tibble(x, rownames = NA)
expect_identical(rownames(out), rownames(x))
expect_identical(out, as_tibble(df))
})

test_that("converting from matrix supports storing row names in a column", {
x <- matrix(1:30, 6, 5, dimnames = list(letters[1:6], LETTERS[1:5]))
df <- tibble(id = letters[1:6], A = 1:6, B = 7:12, C = 13:18, D = 19:24, E = 25:30)
out <- as_tibble(x, rownames = "id")
expect_identical(out, df)
})

0 comments on commit 699dd9a

Please sign in to comment.