diff --git a/NAMESPACE b/NAMESPACE index a886eaa6d..1610b25b3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,6 +9,7 @@ S3method(as_data_frame,"NULL") S3method(as_data_frame,data.frame) S3method(as_data_frame,list) S3method(as_data_frame,matrix) +S3method(as_data_frame,table) S3method(as_data_frame,tbl_df) S3method(format_v,character) S3method(format_v,default) diff --git a/R/dataframe.R b/R/dataframe.R index 1350f793b..726ebf16b 100644 --- a/R/dataframe.R +++ b/R/dataframe.R @@ -103,7 +103,7 @@ lst_ <- function(xs) { #' with more efficient methods for matrices and data frames. #' #' This is an S3 generic. tibble includes methods for data frames (adds tbl_df -#' classes), tbl_dfs (trivial!), lists, and matrices. +#' classes), tbl_dfs (trivial!), lists, matrices, and tables. #' #' @param x A list. Each element of the list must have the same length. #' @param ... Other arguments passed on to individual methods. @@ -188,6 +188,13 @@ as_data_frame.matrix <- function(x, ...) { x } +#' @export +#' @param n Name for count column, default: \code{"n"}. +#' @rdname as_data_frame +as_data_frame.table <- function(x, n = "n", ...) { + as_data_frame(as.data.frame(x, responseName = n, stringsAsFactors = FALSE)) +} + #' @export #' @rdname as_data_frame as_data_frame.NULL <- function(x, ...) { diff --git a/man/as_data_frame.Rd b/man/as_data_frame.Rd index b124ed88a..8db99f117 100644 --- a/man/as_data_frame.Rd +++ b/man/as_data_frame.Rd @@ -6,6 +6,7 @@ \alias{as_data_frame.data.frame} \alias{as_data_frame.list} \alias{as_data_frame.matrix} +\alias{as_data_frame.table} \alias{as_data_frame.tbl_df} \title{Coerce lists and matrices to data frames.} \usage{ @@ -19,6 +20,8 @@ as_data_frame(x, ...) \method{as_data_frame}{matrix}(x, ...) +\method{as_data_frame}{table}(x, n = "n", ...) + \method{as_data_frame}{NULL}(x, ...) } \arguments{ @@ -30,6 +33,8 @@ as_data_frame(x, ...) frame (i.e. all columns are named, and are 1d vectors or lists). You may want to suppress this when you know that you already have a valid data frame and you want to save some time.} + +\item{n}{Name for count column, default: \code{"n"}.} } \description{ \code{as.data.frame} is effectively a thin wrapper around \code{data.frame}, @@ -39,7 +44,7 @@ with more efficient methods for matrices and data frames. } \details{ This is an S3 generic. tibble includes methods for data frames (adds tbl_df -classes), tbl_dfs (trivial!), lists, and matrices. +classes), tbl_dfs (trivial!), lists, matrices, and tables. } \examples{ l <- list(x = 1:500, y = runif(500), z = 500:1) diff --git a/tests/testthat/test-data_frame.R b/tests/testthat/test-data_frame.R index 259e22e33..ee69699d4 100644 --- a/tests/testthat/test-data_frame.R +++ b/tests/testthat/test-data_frame.R @@ -109,6 +109,18 @@ test_that("NULL makes 0 x 0 tbl_df", { expect_equal(dim(nnnull), c(0L, 0L)) }) + +test_that("Can convert tables to data frame", { + mtcars_table <- xtabs(mtcars, formula = ~vs+am+cyl) + + mtcars_tbl <- as_data_frame(mtcars_table) + expect_equal(names(mtcars_tbl), c(names(dimnames(mtcars_table)), "n")) + + mtcars_tbl <- as_data_frame(mtcars_table, "Freq") + expect_equal(names(mtcars_tbl), c(names(dimnames(mtcars_table)), "Freq")) +}) + + # Validation -------------------------------------------------------------- test_that("2d object isn't a valid column", {