Skip to content

Commit

Permalink
Merge branch 'release/0.2-2' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Müller committed Mar 8, 2016
2 parents bb874cf + 3d09e62 commit ec758a4
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 48 deletions.
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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.
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", , "[email protected]", role
= "aut"), person("Romain", "Francois", ,
"[email protected]", role = "aut"), person("Kirill",
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ S3method(type_sum,logical)
S3method(type_sum,matrix)
S3method(type_sum,numeric)
export(add_row)
export(add_rownames)
export(as_data_frame)
export(column_to_rownames)
export(data_frame)
export(data_frame_)
export(dim_desc)
Expand All @@ -43,6 +43,7 @@ export(glimpse)
export(knit_print.trunc_mat)
export(lst)
export(lst_)
export(rownames_to_column)
export(tbl_df)
export(tibble)
export(trunc_mat)
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.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)
===

Expand Down
47 changes: 42 additions & 5 deletions R/dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -194,25 +194,62 @@ as_data_frame.NULL <- function(x, ...) {
as_data_frame(list())
}

#' Convert row names to an explicit variable.
#' 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
#' tbl_df(mtcars)
#' rownames_to_column(mtcars)
#'
#' add_rownames(mtcars)
add_rownames <- function(df, var = "rowname") {
#' 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

as_data_frame(cbind(rn, df))
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
21 changes: 9 additions & 12 deletions R/frame-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,19 @@ frame_data <- function(...) {
))
}

frame_nrow <- n_elements / frame_ncol

# Extract the columns from 'frame_rest'
frame_columns <- lapply(seq_len(frame_ncol), function(i) {
indices <- seq.default(from = i, to = length(frame_rest), by = frame_ncol)
col <- frame_rest[indices]
if (!any(vapply(col, function(x) is.list(x) || length(x) != 1L,
logical(1L)))) {
col <- unlist(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 (any(vapply(col, needs_list_col, logical(1L)))) {
col
} else {
unlist(col)
}
col
})

# 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
Expand Down
4 changes: 4 additions & 0 deletions R/utils.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 0 additions & 22 deletions man/add_rownames.Rd

This file was deleted.

31 changes: 31 additions & 0 deletions man/rownames.Rd

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

35 changes: 30 additions & 5 deletions tests/testthat/test-data_frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,42 @@ test_that("Zero column list makes 0 x 0 tbl_df", {
expect_equal(dim(zero), c(0L, 0L))
})


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")
expect_equal(dim(nnnull), 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"))
})

# Validation --------------------------------------------------------------

test_that("2d object isn't a valid column", {
Expand Down

0 comments on commit ec758a4

Please sign in to comment.