Skip to content

Commit

Permalink
Merge branch 'release/0.3-2' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Müller committed Mar 17, 2016
2 parents 733bcf8 + 161cbc0 commit 9fff95f
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 91 deletions.
6 changes: 5 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Encoding: UTF-8
Package: tibble
Type: Package
Version: 0.3-1
Version: 0.3-2
Date: 2016-03-17
Title: Simple Data Frames
Description: Provides a 'tbl_df' class that offers better checking and
Expand All @@ -22,3 +22,7 @@ License: MIT + file LICENSE
RoxygenNote: 5.0.1
LinkingTo: Rcpp
VignetteBuilder: knitr
Collate: 'RcppExports.R' 'all-equal.r' 'rownames.R' 'dataframe.R'
'frame-data.R' 'glimpse.R' 'repair-column-names.R'
'src-local.r' 'src.r' 'tbl-df.r' 'tibble.R' 'type-sum.r'
'utils-format.r' 'utils.r'
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ export(data_frame_)
export(dim_desc)
export(frame_data)
export(glimpse)
export(has_rownames)
export(knit_print.trunc_mat)
export(lst)
export(lst_)
export(remove_rownames)
export(repair_names)
export(rownames_to_column)
export(tbl_df)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Version 0.3-2 (2016-03-17)
===

- Cleanup for `column_to_rownames()` and `rownames_to_columns()` (#45).
- New `has_rownames()` and `remove_rownames()` (#44).


Version 0.3-1 (2016-03-17)
===

Expand Down
56 changes: 0 additions & 56 deletions R/dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,62 +201,6 @@ as_data_frame.NULL <- function(x, ...) {
as_data_frame(list())
}

#' Conversion between rownames and a column in data frame
#'
#' \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
#' @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(paste("There is a column named", var, "already!"))
}

rn <- as_data_frame(setNames(list(rownames(df)), var))
rownames(df) <- NULL

rn_df <- cbind(rn, df)
class(rn_df) <- class(df)
return (rn_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 (!identical(rownames(df), as.character(seq_len(NROW(df))))) {
stop("This data frame already has row names.")

} else {
if ( !var %in% colnames(df) ) {
stop(paste0("This data frame has no column named ", var, ".") )
}

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
Expand Down
76 changes: 76 additions & 0 deletions R/rownames.R
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
}
2 changes: 1 addition & 1 deletion R/utils-format.r
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ trunc_mat <- function(x, n = NULL, width = NULL, n_extra = 100) {

#' @importFrom stats setNames
shrink_mat <- function(df, width, n_extra, var_names, var_types, rows, n) {
rownames(df) <- NULL
df <- remove_rownames(df)

# Minimum width of each column is 5 "(int)", so we can make a quick first
# pass
Expand Down
21 changes: 17 additions & 4 deletions man/rownames.Rd

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

29 changes: 0 additions & 29 deletions tests/testthat/test-data_frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,35 +74,6 @@ test_that("Zero column list makes 0 x 0 tbl_df", {
})


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( 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!") )
})

test_that("column_to_rownames returns tbl", {
var <- "car"
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_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", {
nnnull <- as_data_frame(NULL)
expect_is(nnnull, "tbl_df")
Expand Down
41 changes: 41 additions & 0 deletions tests/testthat/test-rownames.R
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.") )
})

0 comments on commit 9fff95f

Please sign in to comment.