From bf7b225ace105b43c28222bb287938c32770729c Mon Sep 17 00:00:00 2001 From: Zhilong Jia Date: Thu, 17 Dec 2015 18:08:27 +0000 Subject: [PATCH 01/19] add col2rowname --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/dataframe.R | 28 ++++++++++++++++++++++++++++ man/col2rowname.Rd | 26 ++++++++++++++++++++++++++ tests/testthat/test-data_frame.R | 5 +++++ 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 man/col2rowname.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 3216dafb3..5352d1503 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,4 +25,4 @@ Suggests: Lahman (>= 3.0.1) LazyData: yes License: MIT + file LICENSE -RoxygenNote: 5.0.1.9000 +RoxygenNote: 5.0.1 diff --git a/NAMESPACE b/NAMESPACE index ff002a970..1155d51ca 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -43,6 +43,7 @@ export(add_row) export(add_rownames) export(as.tbl) export(as_data_frame) +export(col2rowname) export(data_frame) export(data_frame_) export(dim_desc) diff --git a/R/dataframe.R b/R/dataframe.R index 7a0edbb86..38da245c5 100644 --- a/R/dataframe.R +++ b/R/dataframe.R @@ -200,6 +200,34 @@ add_rownames <- function(df, var = "rowname") { as_data_frame(cbind(rn, df)) } +#' Convert a column of tbl into rownames of data.frame. +#' +#' Inverted operation of add_rownames +#' +#' @param df Input data frame with rownames. +#' @param var Name of variable to use +#' @export +#' @seealso \code{\link{add_rownames}} +#' @examples +#' tbl_df(mtcars) +#' +#' mtcars_tbl <- add_rownames(mtcars) +#' mtcars_data.frame <- col2rowname(mtcars_tbl) +col2rowname <- function(df, var = "rowname") { + stopifnot(is.data.frame(df)) + + if ( !var %in% colnames(df) ) { + stop(paste("No", var, "column in the colnames.") ) + } + + df <- as.data.frame(df) + rownames(df) <- df[[var]] + df[, var] <- NULL + return (df) + +} + + #' Add a row to a data frame #' #' This is a convenient way to add a single row of data to an existing data diff --git a/man/col2rowname.Rd b/man/col2rowname.Rd new file mode 100644 index 000000000..fe6d3a47a --- /dev/null +++ b/man/col2rowname.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/dataframe.R +\name{col2rowname} +\alias{col2rowname} +\title{Convert a column of tbl into rownames of data.frame.} +\usage{ +col2rowname(df, var = "rowname") +} +\arguments{ +\item{df}{Input data frame with rownames.} + +\item{var}{Name of variable to use} +} +\description{ +Inverted operation of add_rownames +} +\examples{ +tbl_df(mtcars) + +mtcars_tbl <- add_rownames(mtcars) +mtcars_data.frame <- col2rowname(mtcars_tbl) +} +\seealso{ +\code{\link{add_rownames}} +} + diff --git a/tests/testthat/test-data_frame.R b/tests/testthat/test-data_frame.R index 75d9c65d0..4d138643c 100644 --- a/tests/testthat/test-data_frame.R +++ b/tests/testthat/test-data_frame.R @@ -86,6 +86,11 @@ test_that("add_rownames keeps the tbl classes (#882)", { expect_equal( class(res), c("tbl_df","tbl", "data.frame")) }) +test_that("col2rowname returns data.frame.", { + res <- col2rowname( add_rownames( mtcars) ) + expect_equal( class(res), c("data.frame")) +}) + test_that("matrix", { expect_identical(as_data_frame(diag(3L)), as_data_frame(as.data.frame(diag(3L)))) From 314eb7caed49a398279cde22034381cf02365e18 Mon Sep 17 00:00:00 2001 From: Zhilong Jia Date: Fri, 18 Dec 2015 11:03:18 +0000 Subject: [PATCH 02/19] add use_as_rownames. --- NAMESPACE | 2 +- R/dataframe.R | 4 ++-- man/{col2rowname.Rd => use_as_rownames.Rd} | 8 ++++---- tests/testthat/test-data_frame.R | 6 ++++-- 4 files changed, 11 insertions(+), 9 deletions(-) rename man/{col2rowname.Rd => use_as_rownames.Rd} (75%) diff --git a/NAMESPACE b/NAMESPACE index 1155d51ca..4b47dbace 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -43,7 +43,6 @@ export(add_row) export(add_rownames) export(as.tbl) export(as_data_frame) -export(col2rowname) export(data_frame) export(data_frame_) export(dim_desc) @@ -65,6 +64,7 @@ export(tbl_vars) export(tibble) export(trunc_mat) export(type_sum) +export(use_as_rownames) import(assertthat) importFrom(utils,head) importFrom(utils,tail) diff --git a/R/dataframe.R b/R/dataframe.R index 38da245c5..f331866c9 100644 --- a/R/dataframe.R +++ b/R/dataframe.R @@ -212,8 +212,8 @@ add_rownames <- function(df, var = "rowname") { #' tbl_df(mtcars) #' #' mtcars_tbl <- add_rownames(mtcars) -#' mtcars_data.frame <- col2rowname(mtcars_tbl) -col2rowname <- function(df, var = "rowname") { +#' mtcars_data.frame <- use_as_rownames(mtcars_tbl) +use_as_rownames <- function(df, var = "rowname") { stopifnot(is.data.frame(df)) if ( !var %in% colnames(df) ) { diff --git a/man/col2rowname.Rd b/man/use_as_rownames.Rd similarity index 75% rename from man/col2rowname.Rd rename to man/use_as_rownames.Rd index fe6d3a47a..dd2fe1483 100644 --- a/man/col2rowname.Rd +++ b/man/use_as_rownames.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/dataframe.R -\name{col2rowname} -\alias{col2rowname} +\name{use_as_rownames} +\alias{use_as_rownames} \title{Convert a column of tbl into rownames of data.frame.} \usage{ -col2rowname(df, var = "rowname") +use_as_rownames(df, var = "rowname") } \arguments{ \item{df}{Input data frame with rownames.} @@ -18,7 +18,7 @@ Inverted operation of add_rownames tbl_df(mtcars) mtcars_tbl <- add_rownames(mtcars) -mtcars_data.frame <- col2rowname(mtcars_tbl) +mtcars_data.frame <- use_as_rownames(mtcars_tbl) } \seealso{ \code{\link{add_rownames}} diff --git a/tests/testthat/test-data_frame.R b/tests/testthat/test-data_frame.R index 4d138643c..0edf6898b 100644 --- a/tests/testthat/test-data_frame.R +++ b/tests/testthat/test-data_frame.R @@ -86,9 +86,11 @@ test_that("add_rownames keeps the tbl classes (#882)", { expect_equal( class(res), c("tbl_df","tbl", "data.frame")) }) -test_that("col2rowname returns data.frame.", { - res <- col2rowname( add_rownames( mtcars) ) +test_that("use_as_rownames returns data.frame.", { + res <- use_as_rownames( add_rownames( mtcars) ) expect_equal( class(res), c("data.frame")) + expect_equal(rownames(res), rownames(mtcars)) + }) test_that("matrix", { From 8e7a8bb803f1ad33dc386abaaaba7e8011e8a4db Mon Sep 17 00:00:00 2001 From: Zhilong Jia Date: Fri, 18 Dec 2015 12:29:27 +0000 Subject: [PATCH 03/19] add_rownames keeps row names of the input. add more test on use_as_rownames. --- R/dataframe.R | 23 ++++++++++++----------- man/add_rownames.Rd | 13 ++++++++++--- man/use_as_rownames.Rd | 26 -------------------------- tests/testthat/test-data_frame.R | 9 +++++++-- 4 files changed, 29 insertions(+), 42 deletions(-) delete mode 100644 man/use_as_rownames.Rd diff --git a/R/dataframe.R b/R/dataframe.R index f331866c9..60cfdc295 100644 --- a/R/dataframe.R +++ b/R/dataframe.R @@ -182,36 +182,37 @@ as_data_frame.matrix <- function(x, ...) { as_data_frame(as.data.frame(x)) } -#' Convert row names to an explicit variable. +#' Converting between rownames and a column +#' +#' add_rownames: Convert row names to an explicit variable. #' #' @param df Input data frame with rownames. #' @param var Name of variable to use #' @export +#' @rdname add_rownames #' @examples #' tbl_df(mtcars) #' -#' add_rownames(mtcars) +#' mtcars_tbl <- add_rownames(mtcars) add_rownames <- function(df, var = "rowname") { stopifnot(is.data.frame(df)) + if (var %in% colnames(df) ) { + stop(paste("There is a column named", var, "already!")) + } + rn <- as_data_frame(setNames(list(rownames(df)), var)) - rownames(df) <- NULL + rownames(rn) <-rn[[var]] as_data_frame(cbind(rn, df)) } -#' Convert a column of tbl into rownames of data.frame. -#' -#' Inverted operation of add_rownames +#' use_as_rownames: Inverted operation of add_rownames, returning a data.frame #' -#' @param df Input data frame with rownames. -#' @param var Name of variable to use +#' @rdname add_rownames #' @export -#' @seealso \code{\link{add_rownames}} #' @examples -#' tbl_df(mtcars) #' -#' mtcars_tbl <- add_rownames(mtcars) #' mtcars_data.frame <- use_as_rownames(mtcars_tbl) use_as_rownames <- function(df, var = "rowname") { stopifnot(is.data.frame(df)) diff --git a/man/add_rownames.Rd b/man/add_rownames.Rd index 3f9b57965..972a73379 100644 --- a/man/add_rownames.Rd +++ b/man/add_rownames.Rd @@ -2,9 +2,12 @@ % Please edit documentation in R/dataframe.R \name{add_rownames} \alias{add_rownames} -\title{Convert row names to an explicit variable.} +\alias{use_as_rownames} +\title{Converting between rownames and a column} \usage{ add_rownames(df, var = "rowname") + +use_as_rownames(df, var = "rowname") } \arguments{ \item{df}{Input data frame with rownames.} @@ -12,11 +15,15 @@ add_rownames(df, var = "rowname") \item{var}{Name of variable to use} } \description{ -Convert row names to an explicit variable. +add_rownames: Convert row names to an explicit variable. + +use_as_rownames: Inverted operation of add_rownames, returning a data.frame } \examples{ tbl_df(mtcars) -add_rownames(mtcars) +mtcars_tbl <- add_rownames(mtcars) + +mtcars_data.frame <- use_as_rownames(mtcars_tbl) } diff --git a/man/use_as_rownames.Rd b/man/use_as_rownames.Rd deleted file mode 100644 index dd2fe1483..000000000 --- a/man/use_as_rownames.Rd +++ /dev/null @@ -1,26 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/dataframe.R -\name{use_as_rownames} -\alias{use_as_rownames} -\title{Convert a column of tbl into rownames of data.frame.} -\usage{ -use_as_rownames(df, var = "rowname") -} -\arguments{ -\item{df}{Input data frame with rownames.} - -\item{var}{Name of variable to use} -} -\description{ -Inverted operation of add_rownames -} -\examples{ -tbl_df(mtcars) - -mtcars_tbl <- add_rownames(mtcars) -mtcars_data.frame <- use_as_rownames(mtcars_tbl) -} -\seealso{ -\code{\link{add_rownames}} -} - diff --git a/tests/testthat/test-data_frame.R b/tests/testthat/test-data_frame.R index 0edf6898b..f8ce7c83b 100644 --- a/tests/testthat/test-data_frame.R +++ b/tests/testthat/test-data_frame.R @@ -84,13 +84,18 @@ test_that("Zero column list makes 0 x 0 tbl_df", { test_that("add_rownames keeps the tbl classes (#882)", { res <- add_rownames( mtcars, "Make&Model" ) expect_equal( class(res), c("tbl_df","tbl", "data.frame")) + expect_equal( rownames(res), rownames(mtcars) ) }) test_that("use_as_rownames returns data.frame.", { - res <- use_as_rownames( add_rownames( mtcars) ) + var <- "car" + res <- use_as_rownames( add_rownames( mtcars, var), var) expect_equal( class(res), c("data.frame")) expect_equal(rownames(res), rownames(mtcars)) - + expect_equal(res[[var]], NULL) + mtcars$num <- 32:1 + res1 <- use_as_rownames( add_rownames( mtcars), var="num") + expect_equal(rownames(res1), as.character(mtcars$num )) }) test_that("matrix", { From 49da38ef5400481340740de863fa7cc757e78f46 Mon Sep 17 00:00:00 2001 From: Zhilong Jia Date: Fri, 18 Dec 2015 14:52:04 +0000 Subject: [PATCH 04/19] add_rownames return without row name. use_as_rownames stop if there are row names. --- R/dataframe.R | 40 +++++++++++++++++----------- man/{add_rownames.Rd => rownames.Rd} | 15 ++++++++--- tests/testthat/test-data_frame.R | 7 ++++- 3 files changed, 43 insertions(+), 19 deletions(-) rename man/{add_rownames.Rd => rownames.Rd} (63%) diff --git a/R/dataframe.R b/R/dataframe.R index 60cfdc295..e192de250 100644 --- a/R/dataframe.R +++ b/R/dataframe.R @@ -182,18 +182,19 @@ as_data_frame.matrix <- function(x, ...) { as_data_frame(as.data.frame(x)) } -#' Converting between rownames and a column +#' Conversion between rownames and a column in data frame #' -#' add_rownames: Convert row names to an explicit variable. +#' @details add_rownames: Convert row names to an explicit variable. #' #' @param df Input data frame with rownames. #' @param var Name of variable to use #' @export -#' @rdname add_rownames +#' @rdname rownames +#' @return add_rownames: c("tbl_df", "tbl", "data.frame") #' @examples #' tbl_df(mtcars) #' -#' mtcars_tbl <- add_rownames(mtcars) +#' (mtcars_tbl <- add_rownames(mtcars) ) add_rownames <- function(df, var = "rowname") { stopifnot(is.data.frame(df)) @@ -202,30 +203,39 @@ add_rownames <- function(df, var = "rowname") { } rn <- as_data_frame(setNames(list(rownames(df)), var)) - rownames(rn) <-rn[[var]] + rownames(df) <- NULL as_data_frame(cbind(rn, df)) + } -#' use_as_rownames: Inverted operation of add_rownames, returning a data.frame + +#' @details use_as_rownames: Inverted operation of add_rownames, returning a data.frame #' -#' @rdname add_rownames +#' @rdname rownames +#' @return use_as_rownames: data.frame #' @export #' @examples #' -#' mtcars_data.frame <- use_as_rownames(mtcars_tbl) +#' head ( use_as_rownames(mtcars_tbl) ) +#' use_as_rownames <- function(df, var = "rowname") { stopifnot(is.data.frame(df)) - if ( !var %in% colnames(df) ) { - stop(paste("No", var, "column in the colnames.") ) - } + if (!identical(rownames(df), as.character(seq_len(NROW(df))))) { + stop("Row names exists. Nothing is done.") + + } else { + if ( !var %in% colnames(df) ) { + stop(paste("No", var, "column in the colnames.") ) + } - df <- as.data.frame(df) - rownames(df) <- df[[var]] - df[, var] <- NULL - return (df) + df <- as.data.frame(df) + rownames(df) <- df[[var]] + df[, var] <- NULL + return (df) + } } diff --git a/man/add_rownames.Rd b/man/rownames.Rd similarity index 63% rename from man/add_rownames.Rd rename to man/rownames.Rd index 972a73379..366f29aa9 100644 --- a/man/add_rownames.Rd +++ b/man/rownames.Rd @@ -3,7 +3,7 @@ \name{add_rownames} \alias{add_rownames} \alias{use_as_rownames} -\title{Converting between rownames and a column} +\title{Conversion between rownames and a column in data frame} \usage{ add_rownames(df, var = "rowname") @@ -14,7 +14,15 @@ use_as_rownames(df, var = "rowname") \item{var}{Name of variable to use} } +\value{ +add_rownames: c("tbl_df", "tbl", "data.frame") + +use_as_rownames: data.frame +} \description{ +Conversion between rownames and a column in data frame +} +\details{ add_rownames: Convert row names to an explicit variable. use_as_rownames: Inverted operation of add_rownames, returning a data.frame @@ -22,8 +30,9 @@ use_as_rownames: Inverted operation of add_rownames, returning a data.frame \examples{ tbl_df(mtcars) -mtcars_tbl <- add_rownames(mtcars) +(mtcars_tbl <- add_rownames(mtcars) ) + +head ( use_as_rownames(mtcars_tbl) ) -mtcars_data.frame <- use_as_rownames(mtcars_tbl) } diff --git a/tests/testthat/test-data_frame.R b/tests/testthat/test-data_frame.R index f8ce7c83b..af5f335f2 100644 --- a/tests/testthat/test-data_frame.R +++ b/tests/testthat/test-data_frame.R @@ -84,7 +84,8 @@ test_that("Zero column list makes 0 x 0 tbl_df", { test_that("add_rownames keeps the tbl classes (#882)", { res <- add_rownames( mtcars, "Make&Model" ) expect_equal( class(res), c("tbl_df","tbl", "data.frame")) - expect_equal( rownames(res), rownames(mtcars) ) + expect_error(add_rownames( mtcars, "wt"), + paste("There is a column named wt already!") ) }) test_that("use_as_rownames returns data.frame.", { @@ -96,6 +97,10 @@ test_that("use_as_rownames returns data.frame.", { mtcars$num <- 32:1 res1 <- use_as_rownames( add_rownames( mtcars), var="num") expect_equal(rownames(res1), as.character(mtcars$num )) + expect_error(use_as_rownames(res1), "Row names exists. Nothing is done.") + expect_error(use_as_rownames( add_rownames( mtcars, var), "num2"), + paste("No num2 column in the colnames.") ) + }) test_that("matrix", { From 221e2dc0817829b08398125a7ecc5557706a11bb Mon Sep 17 00:00:00 2001 From: Zhilong Jia Date: Sat, 9 Jan 2016 17:59:14 +0000 Subject: [PATCH 05/19] update use_as_rownames description. --- DESCRIPTION | 2 +- R/dataframe.R | 11 ++++++----- man/rownames.Rd | 11 +++++------ tests/testthat/test-data_frame.R | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5352d1503..3216dafb3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,4 +25,4 @@ Suggests: Lahman (>= 3.0.1) LazyData: yes License: MIT + file LICENSE -RoxygenNote: 5.0.1 +RoxygenNote: 5.0.1.9000 diff --git a/R/dataframe.R b/R/dataframe.R index e192de250..1f6db3eac 100644 --- a/R/dataframe.R +++ b/R/dataframe.R @@ -184,7 +184,7 @@ as_data_frame.matrix <- function(x, ...) { #' Conversion between rownames and a column in data frame #' -#' @details add_rownames: Convert row names to an explicit variable. +#' \code{add_rownames} Convert row names to an explicit variable. #' #' @param df Input data frame with rownames. #' @param var Name of variable to use @@ -210,7 +210,8 @@ add_rownames <- function(df, var = "rowname") { } -#' @details use_as_rownames: Inverted operation of add_rownames, returning a data.frame +#' \code{use_as_rownames} Convert a column variable to row names. This is an +#' inverted operation of add_rownames, returning a data.frame #' #' @rdname rownames #' @return use_as_rownames: data.frame @@ -223,14 +224,14 @@ use_as_rownames <- function(df, var = "rowname") { stopifnot(is.data.frame(df)) if (!identical(rownames(df), as.character(seq_len(NROW(df))))) { - stop("Row names exists. Nothing is done.") + stop("This data frame already has row names.") } else { if ( !var %in% colnames(df) ) { - stop(paste("No", var, "column in the colnames.") ) + stop(paste0("This data frame has no column named ", var, ".") ) } - df <- as.data.frame(df) + df <- as_data_frame(df) rownames(df) <- df[[var]] df[, var] <- NULL diff --git a/man/rownames.Rd b/man/rownames.Rd index 366f29aa9..7b1f28673 100644 --- a/man/rownames.Rd +++ b/man/rownames.Rd @@ -20,12 +20,10 @@ add_rownames: c("tbl_df", "tbl", "data.frame") use_as_rownames: data.frame } \description{ -Conversion between rownames and a column in data frame -} -\details{ -add_rownames: Convert row names to an explicit variable. +\code{add_rownames} Convert row names to an explicit variable. -use_as_rownames: Inverted operation of add_rownames, returning a data.frame +\code{use_as_rownames} Convert a column variable to row names. This is an +inverted operation of add_rownames, returning a data.frame } \examples{ tbl_df(mtcars) @@ -33,6 +31,7 @@ tbl_df(mtcars) (mtcars_tbl <- add_rownames(mtcars) ) head ( use_as_rownames(mtcars_tbl) ) - +df <- mtcars_tbl +var = "rowname" } diff --git a/tests/testthat/test-data_frame.R b/tests/testthat/test-data_frame.R index af5f335f2..9ecef8396 100644 --- a/tests/testthat/test-data_frame.R +++ b/tests/testthat/test-data_frame.R @@ -94,7 +94,7 @@ test_that("use_as_rownames returns data.frame.", { expect_equal( class(res), c("data.frame")) expect_equal(rownames(res), rownames(mtcars)) expect_equal(res[[var]], NULL) - mtcars$num <- 32:1 + mtcars$num <- rev(seq_len(nrow(mtcars))) res1 <- use_as_rownames( add_rownames( mtcars), var="num") expect_equal(rownames(res1), as.character(mtcars$num )) expect_error(use_as_rownames(res1), "Row names exists. Nothing is done.") From 026b1a4b4488020acd972b5676fefd85120ee157 Mon Sep 17 00:00:00 2001 From: Zhilong Jia Date: Sat, 9 Jan 2016 18:27:50 +0000 Subject: [PATCH 06/19] debug test of use_as_rownames. --- R/dataframe.R | 2 +- man/rownames.Rd | 3 +-- tests/testthat/test-data_frame.R | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/R/dataframe.R b/R/dataframe.R index 1f6db3eac..cf5141711 100644 --- a/R/dataframe.R +++ b/R/dataframe.R @@ -231,7 +231,7 @@ use_as_rownames <- function(df, var = "rowname") { stop(paste0("This data frame has no column named ", var, ".") ) } - df <- as_data_frame(df) + df <- as.data.frame(df) rownames(df) <- df[[var]] df[, var] <- NULL diff --git a/man/rownames.Rd b/man/rownames.Rd index 7b1f28673..711fc89e3 100644 --- a/man/rownames.Rd +++ b/man/rownames.Rd @@ -31,7 +31,6 @@ tbl_df(mtcars) (mtcars_tbl <- add_rownames(mtcars) ) head ( use_as_rownames(mtcars_tbl) ) -df <- mtcars_tbl -var = "rowname" + } diff --git a/tests/testthat/test-data_frame.R b/tests/testthat/test-data_frame.R index 9ecef8396..61740e7e3 100644 --- a/tests/testthat/test-data_frame.R +++ b/tests/testthat/test-data_frame.R @@ -97,9 +97,9 @@ test_that("use_as_rownames returns data.frame.", { mtcars$num <- rev(seq_len(nrow(mtcars))) res1 <- use_as_rownames( add_rownames( mtcars), var="num") expect_equal(rownames(res1), as.character(mtcars$num )) - expect_error(use_as_rownames(res1), "Row names exists. Nothing is done.") + expect_error(use_as_rownames(res1), "This data frame already has row names.") expect_error(use_as_rownames( add_rownames( mtcars, var), "num2"), - paste("No num2 column in the colnames.") ) + paste("This data frame has no column named num2.") ) }) From 1eaaf5d844bd9643e17d5032058d1dedb7cd79b0 Mon Sep 17 00:00:00 2001 From: Zhilong JIA Date: Sun, 10 Jan 2016 00:19:59 +0000 Subject: [PATCH 07/19] add rownames_to_column; column_to_rownames returns tbl instead of data.frame. --- NAMESPACE | 3 ++- R/dataframe.R | 19 ++++++++++--------- man/rownames.Rd | 22 +++++++++++----------- tests/testthat/test-data_frame.R | 16 ++++++++-------- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 4b47dbace..8f8f78c6d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -43,6 +43,7 @@ export(add_row) export(add_rownames) export(as.tbl) export(as_data_frame) +export(column_to_rownames) export(data_frame) export(data_frame_) export(dim_desc) @@ -53,6 +54,7 @@ export(is.tbl) export(knit_print.trunc_mat) export(lst) export(lst_) +export(rownames_to_column) export(same_src) export(src) export(src_df) @@ -64,7 +66,6 @@ export(tbl_vars) export(tibble) export(trunc_mat) export(type_sum) -export(use_as_rownames) import(assertthat) importFrom(utils,head) importFrom(utils,tail) diff --git a/R/dataframe.R b/R/dataframe.R index cf5141711..7c3ce5d59 100644 --- a/R/dataframe.R +++ b/R/dataframe.R @@ -184,17 +184,18 @@ as_data_frame.matrix <- function(x, ...) { #' Conversion between rownames and a column in data frame #' -#' \code{add_rownames} Convert row names to an explicit variable. +#' \code{add_rownames or rownames_to_column} convert row names to an explicit variable. #' #' @param df Input data frame with rownames. #' @param var Name of variable to use #' @export #' @rdname rownames -#' @return add_rownames: c("tbl_df", "tbl", "data.frame") #' @examples #' tbl_df(mtcars) #' #' (mtcars_tbl <- add_rownames(mtcars) ) +#' +#' rownames_to_column(mtcars) add_rownames <- function(df, var = "rowname") { stopifnot(is.data.frame(df)) @@ -209,18 +210,20 @@ add_rownames <- function(df, var = "rowname") { } +#' @rdname rownames +#' @export +rownames_to_column <- add_rownames -#' \code{use_as_rownames} Convert a column variable to row names. This is an -#' inverted operation of add_rownames, returning a data.frame +#' \code{column_to_rownames} convert a column variable to row names. This is an +#' inverted operation of add_rownames or rownames_to_column. #' #' @rdname rownames -#' @return use_as_rownames: data.frame #' @export #' @examples #' -#' head ( use_as_rownames(mtcars_tbl) ) +#' column_to_rownames(mtcars_tbl) #' -use_as_rownames <- function(df, var = "rowname") { +column_to_rownames <- function(df, var = "rowname") { stopifnot(is.data.frame(df)) if (!identical(rownames(df), as.character(seq_len(NROW(df))))) { @@ -231,8 +234,6 @@ use_as_rownames <- function(df, var = "rowname") { stop(paste0("This data frame has no column named ", var, ".") ) } - df <- as.data.frame(df) - rownames(df) <- df[[var]] df[, var] <- NULL return (df) diff --git a/man/rownames.Rd b/man/rownames.Rd index 711fc89e3..73aad99e1 100644 --- a/man/rownames.Rd +++ b/man/rownames.Rd @@ -2,35 +2,35 @@ % Please edit documentation in R/dataframe.R \name{add_rownames} \alias{add_rownames} -\alias{use_as_rownames} +\alias{column_to_rownames} +\alias{rownames_to_column} \title{Conversion between rownames and a column in data frame} \usage{ add_rownames(df, var = "rowname") -use_as_rownames(df, var = "rowname") +rownames_to_column(df, var = "rowname") + +column_to_rownames(df, var = "rowname") } \arguments{ \item{df}{Input data frame with rownames.} \item{var}{Name of variable to use} } -\value{ -add_rownames: c("tbl_df", "tbl", "data.frame") - -use_as_rownames: data.frame -} \description{ -\code{add_rownames} Convert row names to an explicit variable. +\code{add_rownames or rownames_to_column} convert row names to an explicit variable. -\code{use_as_rownames} Convert a column variable to row names. This is an -inverted operation of add_rownames, returning a data.frame +\code{column_to_rownames} convert a column variable to row names. This is an +inverted operation of add_rownames or rownames_to_column. } \examples{ tbl_df(mtcars) (mtcars_tbl <- add_rownames(mtcars) ) -head ( use_as_rownames(mtcars_tbl) ) +rownames_to_column(mtcars) + +column_to_rownames(mtcars_tbl) } diff --git a/tests/testthat/test-data_frame.R b/tests/testthat/test-data_frame.R index 61740e7e3..57dad4871 100644 --- a/tests/testthat/test-data_frame.R +++ b/tests/testthat/test-data_frame.R @@ -88,17 +88,17 @@ test_that("add_rownames keeps the tbl classes (#882)", { paste("There is a column named wt already!") ) }) -test_that("use_as_rownames returns data.frame.", { +test_that("column_to_rownames returns tbl", { var <- "car" - res <- use_as_rownames( add_rownames( mtcars, var), var) - expect_equal( class(res), c("data.frame")) + res <- column_to_rownames( add_rownames( mtcars, var), var) + expect_equal( class(res), c("tbl_df", "tbl", "data.frame")) expect_equal(rownames(res), rownames(mtcars)) - expect_equal(res[[var]], NULL) + expect_error(res[[var]], "Error : Unknown name\n") mtcars$num <- rev(seq_len(nrow(mtcars))) - res1 <- use_as_rownames( add_rownames( mtcars), var="num") - expect_equal(rownames(res1), as.character(mtcars$num )) - expect_error(use_as_rownames(res1), "This data frame already has row names.") - expect_error(use_as_rownames( add_rownames( mtcars, var), "num2"), + res1 <- column_to_rownames( add_rownames( mtcars), var="num") + 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( add_rownames( mtcars, var), "num2"), paste("This data frame has no column named num2.") ) }) From c71047969d3882e5a6c530afabac494ad45a51a3 Mon Sep 17 00:00:00 2001 From: Zhilong Jia Date: Sun, 10 Jan 2016 12:38:01 +0000 Subject: [PATCH 08/19] add rownames_to_column test. --- tests/testthat/test-data_frame.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/testthat/test-data_frame.R b/tests/testthat/test-data_frame.R index 57dad4871..510399014 100644 --- a/tests/testthat/test-data_frame.R +++ b/tests/testthat/test-data_frame.R @@ -86,6 +86,10 @@ test_that("add_rownames keeps the tbl classes (#882)", { expect_equal( class(res), c("tbl_df","tbl", "data.frame")) expect_error(add_rownames( mtcars, "wt"), paste("There is a column named wt already!") ) + res1 <- rownames_to_column( mtcars, "Make&Model" ) + expect_equal( class(res1), c("tbl_df","tbl", "data.frame")) + expect_error(rownames_to_column( mtcars, "wt"), + paste("There is a column named wt already!") ) }) test_that("column_to_rownames returns tbl", { From 9c17a0b3f1e18f0167da977b9445b6fc6714ec9a Mon Sep 17 00:00:00 2001 From: Zhilong JIA Date: Mon, 25 Jan 2016 21:28:35 +0000 Subject: [PATCH 09/19] Delete add_rownames and rownames_to_column doesnot touch the class of data. --- NAMESPACE | 1 - R/dataframe.R | 20 +++++++++----------- man/rownames.Rd | 13 +++++-------- tests/testthat/test-data_frame.R | 19 ++++++++++--------- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 8f8f78c6d..a413fa467 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -40,7 +40,6 @@ S3method(type_sum,logical) S3method(type_sum,matrix) S3method(type_sum,numeric) export(add_row) -export(add_rownames) export(as.tbl) export(as_data_frame) export(column_to_rownames) diff --git a/R/dataframe.R b/R/dataframe.R index 7c3ce5d59..bcf223a8b 100644 --- a/R/dataframe.R +++ b/R/dataframe.R @@ -184,7 +184,7 @@ as_data_frame.matrix <- function(x, ...) { #' Conversion between rownames and a column in data frame #' -#' \code{add_rownames or rownames_to_column} convert row names to an explicit variable. +#' \code{rownames_to_column} convert row names to an explicit variable. #' #' @param df Input data frame with rownames. #' @param var Name of variable to use @@ -193,10 +193,11 @@ as_data_frame.matrix <- function(x, ...) { #' @examples #' tbl_df(mtcars) #' -#' (mtcars_tbl <- add_rownames(mtcars) ) -#' +#' (mtcars_tbl <- rownames_to_column(tbl_df(mtcars) )) #' rownames_to_column(mtcars) -add_rownames <- function(df, var = "rowname") { +#' +rownames_to_column <- function(df, var = "rowname") { + stopifnot(is.data.frame(df)) if (var %in% colnames(df) ) { @@ -206,16 +207,13 @@ add_rownames <- function(df, var = "rowname") { rn <- as_data_frame(setNames(list(rownames(df)), var)) rownames(df) <- NULL - as_data_frame(cbind(rn, df)) - + rn_df <- cbind(rn, df) + class(rn_df) <- class(df) + return (rn_df) } -#' @rdname rownames -#' @export -rownames_to_column <- add_rownames - #' \code{column_to_rownames} convert a column variable to row names. This is an -#' inverted operation of add_rownames or rownames_to_column. +#' inverted operation of rownames_to_column. #' #' @rdname rownames #' @export diff --git a/man/rownames.Rd b/man/rownames.Rd index 73aad99e1..c93885022 100644 --- a/man/rownames.Rd +++ b/man/rownames.Rd @@ -1,13 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/dataframe.R -\name{add_rownames} -\alias{add_rownames} +\name{rownames_to_column} \alias{column_to_rownames} \alias{rownames_to_column} \title{Conversion between rownames and a column in data frame} \usage{ -add_rownames(df, var = "rowname") - rownames_to_column(df, var = "rowname") column_to_rownames(df, var = "rowname") @@ -18,18 +15,18 @@ column_to_rownames(df, var = "rowname") \item{var}{Name of variable to use} } \description{ -\code{add_rownames or rownames_to_column} convert row names to an explicit variable. +\code{rownames_to_column} convert row names to an explicit variable. \code{column_to_rownames} convert a column variable to row names. This is an -inverted operation of add_rownames or rownames_to_column. +inverted operation of rownames_to_column. } \examples{ tbl_df(mtcars) -(mtcars_tbl <- add_rownames(mtcars) ) - +(mtcars_tbl <- rownames_to_column(tbl_df(mtcars) )) rownames_to_column(mtcars) + column_to_rownames(mtcars_tbl) } diff --git a/tests/testthat/test-data_frame.R b/tests/testthat/test-data_frame.R index 510399014..4bdfbb455 100644 --- a/tests/testthat/test-data_frame.R +++ b/tests/testthat/test-data_frame.R @@ -81,28 +81,29 @@ test_that("Zero column list makes 0 x 0 tbl_df", { expect_equal(dim(zero), c(0L, 0L)) }) -test_that("add_rownames keeps the tbl classes (#882)", { - res <- add_rownames( mtcars, "Make&Model" ) - expect_equal( class(res), c("tbl_df","tbl", "data.frame")) - expect_error(add_rownames( mtcars, "wt"), +test_that("rownames_to_column keeps the tbl classes (#882)", { + res <- rownames_to_column( mtcars, "Make&Model" ) + 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( mtcars, "Make&Model" ) - expect_equal( class(res1), c("tbl_df","tbl", "data.frame")) + expect_equal( class(res1), class(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" - res <- column_to_rownames( add_rownames( mtcars, var), var) - expect_equal( class(res), c("tbl_df", "tbl", "data.frame")) + mtcars <- tbl_df(mtcars) + res <- column_to_rownames( rownames_to_column( mtcars, var), var) + expect_equal( class(res), class(mtcars) ) expect_equal(rownames(res), rownames(mtcars)) expect_error(res[[var]], "Error : Unknown name\n") mtcars$num <- rev(seq_len(nrow(mtcars))) - res1 <- column_to_rownames( add_rownames( mtcars), var="num") + res1 <- column_to_rownames( rownames_to_column( mtcars), var="num") 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( add_rownames( mtcars, var), "num2"), + expect_error(column_to_rownames( rownames_to_column( mtcars, var), "num2"), paste("This data frame has no column named num2.") ) }) From 8b2542c330c8b9d6b8b8334dcab3f53d49c43325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Wed, 2 Mar 2016 09:44:41 +0100 Subject: [PATCH 10/19] rename --- R/frame-data.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/frame-data.R b/R/frame-data.R index f17f0f820..fe2644f53 100644 --- a/R/frame-data.R +++ b/R/frame-data.R @@ -67,7 +67,7 @@ frame_data <- function(...) { frame_nrow <- n_elements / frame_ncol # Extract the columns from 'frame_rest' - frame_columns <- lapply(seq_len(frame_ncol), function(i) { + frame_col <- lapply(seq_len(frame_ncol), function(i) { indices <- seq.default(from = i, to = length(frame_rest), by = frame_ncol) col <- frame_rest[indices] if (all(vapply(col, length, integer(1L)) == 1L)) { @@ -77,8 +77,8 @@ frame_data <- function(...) { }) # Create a tbl_df and return it - names(frame_columns) <- frame_names - as_data_frame(frame_columns) + names(frame_col) <- frame_names + as_data_frame(frame_col) } #' @rdname frame_data From 3023b3c95b7cbb4bd85bc510d80575c99339de85 Mon Sep 17 00:00:00 2001 From: hadley Date: Thu, 12 Nov 2015 08:03:38 -0600 Subject: [PATCH 11/19] Support list-cols in frame_data(). Cherry-picked from hadley/dplyr@25e95da83. --- R/frame-data.R | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/R/frame-data.R b/R/frame-data.R index fe2644f53..8019c1376 100644 --- a/R/frame-data.R +++ b/R/frame-data.R @@ -64,16 +64,10 @@ frame_data <- function(...) { )) } - frame_nrow <- n_elements / frame_ncol - - # Extract the columns from 'frame_rest' - frame_col <- lapply(seq_len(frame_ncol), function(i) { - indices <- seq.default(from = i, to = length(frame_rest), by = frame_ncol) - col <- frame_rest[indices] - if (all(vapply(col, length, integer(1L)) == 1L)) { - col <- unlist(col) - } - col + frame_mat <- matrix(frame_rest, ncol = frame_ncol, byrow = TRUE) + frame_col <- lapply(seq_len(ncol(frame_mat)), function(i) { + col <- frame_mat[, i] + if (is.list(col[[1]])) col else unlist(col) }) # Create a tbl_df and return it From adb9400113eb0b310edc945ac13061d13b7fae6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Wed, 2 Mar 2016 10:00:25 +0100 Subject: [PATCH 12/19] also handle non-atomic input --- R/frame-data.R | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/R/frame-data.R b/R/frame-data.R index 8019c1376..51b6a48c7 100644 --- a/R/frame-data.R +++ b/R/frame-data.R @@ -67,7 +67,12 @@ frame_data <- function(...) { frame_mat <- matrix(frame_rest, ncol = frame_ncol, byrow = TRUE) frame_col <- lapply(seq_len(ncol(frame_mat)), function(i) { col <- frame_mat[, i] - if (is.list(col[[1]])) col else unlist(col) + if (any(vapply(col, function(x) is.list(x) || length(x) != 1L, + logical(1L)))) { + col + } else { + unlist(col) + } }) # Create a tbl_df and return it From e35f2d1a48ab97b2f70965586ce82dd126baeb96 Mon Sep 17 00:00:00 2001 From: Zhilong JIA Date: Tue, 8 Mar 2016 21:15:50 +0800 Subject: [PATCH 13/19] merge conflict from krlmlr manually. --- NAMESPACE | 14 -------------- R/dataframe.R | 3 --- tests/testthat/test-data_frame.R | 12 +++++++----- 3 files changed, 7 insertions(+), 22 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 70d263a9d..2d2b3cbf7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -33,11 +33,6 @@ S3method(type_sum,logical) S3method(type_sum,matrix) S3method(type_sum,numeric) export(add_row) -<<<<<<< HEAD -export(as.tbl) -======= -export(add_rownames) ->>>>>>> krlmlr/master export(as_data_frame) export(column_to_rownames) export(data_frame) @@ -48,16 +43,7 @@ export(glimpse) export(knit_print.trunc_mat) export(lst) export(lst_) -<<<<<<< HEAD export(rownames_to_column) -export(same_src) -export(src) -export(src_df) -export(src_local) -export(src_tbls) -export(tbl) -======= ->>>>>>> krlmlr/master export(tbl_df) export(tibble) export(trunc_mat) diff --git a/R/dataframe.R b/R/dataframe.R index 5ea3a18c8..74f478f06 100644 --- a/R/dataframe.R +++ b/R/dataframe.R @@ -201,11 +201,8 @@ as_data_frame.NULL <- function(x, ...) { #' @param df Input data frame with rownames. #' @param var Name of variable to use #' @export -<<<<<<< HEAD #' @rdname rownames -======= #' @importFrom stats setNames ->>>>>>> krlmlr/master #' @examples #' tbl_df(mtcars) #' diff --git a/tests/testthat/test-data_frame.R b/tests/testthat/test-data_frame.R index 24f46a774..86c0f97ff 100644 --- a/tests/testthat/test-data_frame.R +++ b/tests/testthat/test-data_frame.R @@ -73,7 +73,7 @@ test_that("Zero column list makes 0 x 0 tbl_df", { expect_equal(dim(zero), c(0L, 0L)) }) -<<<<<<< HEAD + test_that("rownames_to_column keeps the tbl classes (#882)", { res <- rownames_to_column( mtcars, "Make&Model" ) expect_equal( class(res), class(mtcars) ) @@ -98,18 +98,20 @@ test_that("column_to_rownames returns tbl", { 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.") ) + }) + -======= test_that("NULL makes 0 x 0 tbl_df", { nnnull <- as_data_frame(NULL) expect_is(nnnull, "tbl_df") expect_equal(dim(nnnull), c(0L, 0L)) }) -test_that("add_rownames keeps the tbl classes (#882)", { - res <- add_rownames( mtcars, "Make&Model" ) +test_that("rownames_to_column keeps the tbl classes (#882)", { + mtcars <- tbl_df(mtcars) + res <- rownames_to_column( mtcars, "Make&Model" ) expect_equal( class(res), c("tbl_df","tbl", "data.frame")) ->>>>>>> krlmlr/master + }) # Validation -------------------------------------------------------------- From 71fe08d714f42807838b5421217e247a683de2fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Tue, 8 Mar 2016 21:44:10 +0100 Subject: [PATCH 14/19] tweak documentation --- R/dataframe.R | 8 +++----- man/rownames.Rd | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/R/dataframe.R b/R/dataframe.R index 74f478f06..1350f793b 100644 --- a/R/dataframe.R +++ b/R/dataframe.R @@ -204,11 +204,10 @@ as_data_frame.NULL <- function(x, ...) { #' @rdname rownames #' @importFrom stats setNames #' @examples -#' tbl_df(mtcars) -#' -#' (mtcars_tbl <- rownames_to_column(tbl_df(mtcars) )) #' 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)) @@ -226,14 +225,13 @@ rownames_to_column <- function(df, var = "rowname") { } #' \code{column_to_rownames} convert a column variable to row names. This is an -#' inverted operation of rownames_to_column. +#' 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)) diff --git a/man/rownames.Rd b/man/rownames.Rd index c93885022..30fbc6351 100644 --- a/man/rownames.Rd +++ b/man/rownames.Rd @@ -18,16 +18,14 @@ column_to_rownames(df, var = "rowname") \code{rownames_to_column} convert row names to an explicit variable. \code{column_to_rownames} convert a column variable to row names. This is an -inverted operation of rownames_to_column. +inverted operation of \code{rownames_to_column}. } \examples{ -tbl_df(mtcars) - -(mtcars_tbl <- rownames_to_column(tbl_df(mtcars) )) rownames_to_column(mtcars) +mtcars_tbl <- rownames_to_column(tbl_df(mtcars)) +mtcars_tbl column_to_rownames(mtcars_tbl) - } From cfa50c166bc48f07674c3197a58f0de32107cbdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Tue, 8 Mar 2016 21:47:21 +0100 Subject: [PATCH 15/19] tweak tests --- tests/testthat/test-data_frame.R | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/tests/testthat/test-data_frame.R b/tests/testthat/test-data_frame.R index 86c0f97ff..259e22e33 100644 --- a/tests/testthat/test-data_frame.R +++ b/tests/testthat/test-data_frame.R @@ -79,8 +79,8 @@ test_that("rownames_to_column keeps the tbl classes (#882)", { 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( mtcars, "Make&Model" ) - expect_equal( class(res1), class(mtcars) ) + res1 <- rownames_to_column( tbl_df(mtcars), "Make&Model" ) + expect_equal( class(res1), class(tbl_df(mtcars)) ) expect_error(rownames_to_column( mtcars, "wt"), paste("There is a column named wt already!") ) }) @@ -91,14 +91,16 @@ test_that("column_to_rownames returns tbl", { res <- column_to_rownames( rownames_to_column( mtcars, var), var) expect_equal( class(res), class(mtcars) ) expect_equal(rownames(res), rownames(mtcars)) - expect_error(res[[var]], "Error : Unknown name\n") + 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_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.") ) - }) +}) test_that("NULL makes 0 x 0 tbl_df", { @@ -107,13 +109,6 @@ test_that("NULL makes 0 x 0 tbl_df", { expect_equal(dim(nnnull), c(0L, 0L)) }) -test_that("rownames_to_column keeps the tbl classes (#882)", { - mtcars <- tbl_df(mtcars) - res <- rownames_to_column( mtcars, "Make&Model" ) - expect_equal( class(res), c("tbl_df","tbl", "data.frame")) - -}) - # Validation -------------------------------------------------------------- test_that("2d object isn't a valid column", { From 8ed51d869a79f0725490146c5056ecd96457f0d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Tue, 8 Mar 2016 21:50:11 +0100 Subject: [PATCH 16/19] extract needs_list_col() --- R/frame-data.R | 3 +-- R/utils.r | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/R/frame-data.R b/R/frame-data.R index 51b6a48c7..2a465abd1 100644 --- a/R/frame-data.R +++ b/R/frame-data.R @@ -67,8 +67,7 @@ frame_data <- function(...) { frame_mat <- matrix(frame_rest, ncol = frame_ncol, byrow = TRUE) frame_col <- lapply(seq_len(ncol(frame_mat)), function(i) { col <- frame_mat[, i] - if (any(vapply(col, function(x) is.list(x) || length(x) != 1L, - logical(1L)))) { + if (any(vapply(col, needs_list_col, logical(1L)))) { col } else { unlist(col) diff --git a/R/utils.r b/R/utils.r index 52821743f..acf7b1f1d 100644 --- a/R/utils.r +++ b/R/utils.r @@ -15,3 +15,7 @@ is_1d <- function(x) { # dimension check is for matrices and data.frames (is_atomic(x) || is.list(x)) && length(dim(x)) <= 1 } + +needs_list_col <- function(x) { + is.list(x) || length(x) != 1L +} From b517fede7e5dc7d148ffe21f9a3dd152cb63410b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Tue, 8 Mar 2016 21:58:10 +0100 Subject: [PATCH 17/19] bump version to 0.2-2 --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index a2fbdf400..12c0db747 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Encoding: UTF-8 Package: tibble Type: Package -Version: 0.2-1 +Version: 0.2-2 Date: 2016-03-08 Title: Simple data frames Description: Data frames and data sources in "dplyr" style. From eb9f32b74793b3971b47d0a1c07cfc089503783f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Tue, 8 Mar 2016 21:58:24 +0100 Subject: [PATCH 18/19] NEWS --- NEWS.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/NEWS.md b/NEWS.md index 6161f9f62..b4a836fde 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +Version 0.2-2 (2016-03-08) +=== + +- `frame_data()` now also creates a list column if one of the entries is a list (#32). +- New `rownames_to_column()` and `column_to_rownames()` functions, replace `add_rownames()` (#11, @zhilongjia). + + Version 0.2-1 (2016-03-08) === From 3d09e62e60463ab804aecb72bbb4ba9f43437e64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Tue, 8 Mar 2016 22:03:00 +0100 Subject: [PATCH 19/19] edit title and description @hadley: PTAL --- DESCRIPTION | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 12c0db747..544f96508 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,8 +3,9 @@ Package: tibble Type: Package Version: 0.2-2 Date: 2016-03-08 -Title: Simple data frames -Description: Data frames and data sources in "dplyr" style. +Title: Simple Data Frames +Description: Provides a 'tbl_df' class that offers better checking and + printing capabilities than traditional data frames. Authors@R: c( person("Hadley", "Wickham", , "hadley@rstudio.com", role = "aut"), person("Romain", "Francois", , "romain@r-enthusiasts.com", role = "aut"), person("Kirill",