Browse files

New parameter .id for ldply() to specify name of index column (defaul…

…t: .id); fixes # 140; fixes # 107
  • Loading branch information...
1 parent bd8cba3 commit cedabf751a59a5d70b964dd7e6d31733088ae290 @krlmlr krlmlr committed with krlmlr Apr 20, 2013
Showing with 45 additions and 14 deletions.
  1. +9 −6 R/ldply.r
  2. +10 −5 R/list-to-dataframe.r
  3. +13 −0 inst/tests/test-data-frame.r
  4. +13 −3 inst/tests/test-simplify-df.r
View
15 R/ldply.r
@@ -1,18 +1,21 @@
#' Split list, apply function, and return results in a data frame.
-#'
-#' For each element of a list, apply function then combine results into a data
-#' frame.
-#'
+#'
+#' For each element of a list, apply function then combine results into
+#' a data frame.
+#'
#' @template ply
#' @template l-
#' @template -d
+#' @param .id name of the index column (used if \code{.data} is a
+#' named list), defaults to \code{".id"}. Pass \code{NULL} to avoid
+#' creation of the index column
#' @export
ldply <- function(.data, .fun = NULL, ..., .progress = "none", .inform = FALSE,
- .parallel = FALSE, .paropts = NULL) {
+ .parallel = FALSE, .paropts = NULL, .id = ".id") {
if (!inherits(.data, "split")) .data <- as.list(.data)
res <- llply(.data = .data, .fun = .fun, ...,
.progress = .progress, .inform = .inform,
.parallel = .parallel, .paropts = .paropts)
- list_to_dataframe(res, attr(.data, "split_labels"))
+ list_to_dataframe(res, attr(.data, "split_labels"), .id)
}
View
15 R/list-to-dataframe.r
@@ -1,14 +1,16 @@
#' List to data frame.
#'
-#' Reduce/simplify a list of homogenous objects to a data frame.
-#' All \code{NULL} entries are removed. Remaining entries must be all atomic
+#' Reduce/simplify a list of homogenous objects to a data frame. All
+#' \code{NULL} entries are removed. Remaining entries must be all atomic
#' or all data frames.
#'
#' @family list simplification functions
#' @param res list of input data
#' @param labels a data frame of labels, one row for each element of res
+#' @param idname the name of the index column, \code{NULL} for no index
+#' column
#' @keywords internal
-list_to_dataframe <- function(res, labels = NULL) {
+list_to_dataframe <- function(res, labels = NULL, idname = NULL) {
null <- vapply(res, is.null, logical(1))
res <- res[!null]
if (length(res) == 0) return(data.frame())
@@ -17,8 +19,11 @@ list_to_dataframe <- function(res, labels = NULL) {
stopifnot(nrow(labels) == length(null))
labels <- labels[!null, , drop = FALSE]
}
- if (is.null(labels) && !is.null(names(res))) {
- labels <- data.frame(.id = names(res), stringsAsFactors = FALSE)
+ names.res <- names(res)
+ if (!is.null(idname) && is.null(labels) && !is.null(names.res)) {
+ stopifnot(length(idname) == 1)
+ labels <- data.frame(.id = names.res, stringsAsFactors = FALSE)
+ names(labels) <- idname
}
# Figure out how to turn elements into a data frame
View
13 inst/tests/test-data-frame.r
@@ -60,3 +60,16 @@ test_that("label variables always preserved", {
})
+
+# Test for #140
+test_that(".id column can be renamed", {
+ l <- llply(1:4, function(i) rep(i, i))
+ names(l) <- 1:4
+ f <- function(l) data.frame(sum=sum(unlist(l)))
+
+ out1 <- ldply(l, f)
+ out2 <- ldply(l, f, .id='x')
+
+ expect_equal(names(out1), c('.id', 'sum'))
+ expect_equal(names(out2), c('x', 'sum'))
+})
View
16 inst/tests/test-simplify-df.r
@@ -121,14 +121,24 @@ test_that("correct number of rows outputted", {
expect_that(nrow(res), equals(3))
})
-
-test_that("matrices converted to data frames", {
+test_that("matrices converted to data frames, without id column", {
mat <- matrix(1:20, ncol = 4)
colnames(mat) <- letters[1:4]
li <- list(a = mat, b = mat)
df <- list_to_dataframe(li)
expect_equal(nrow(df), 2 * nrow(mat))
- expect_equal(names(df), c(".id", "a", "b", "c", "d"))
+ expect_equal(names(df), c("a", "b", "c", "d"))
+})
+
+test_that("matrices converted to data frames, with id column", {
+ mat <- matrix(1:20, ncol = 4)
+ colnames(mat) <- letters[1:4]
+
+ li <- list(a = mat, b = mat)
+ df <- list_to_dataframe(li, idname="my-id")
+
+ expect_equal(nrow(df), 2 * nrow(mat))
+ expect_equal(names(df), c("my-id", "a", "b", "c", "d"))
})

0 comments on commit cedabf7

Please sign in to comment.