Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

…t: .n); fixes # 142
  • Loading branch information...
commit 40945665266203b9766da8bc93ddba95cc205046 1 parent cedabf7
@krlmlr krlmlr authored
View
8 R/rdply.r
@@ -13,6 +13,8 @@
#' @param .n number of times to evaluate the expression
#' @param .expr expression to evaluate
#' @param .progress name of the progress bar to use, see \code{\link{create_progress_bar}}
+#' @param .id name of the index column, defaults to \code{".n"}. Pass
+#' \code{NULL} to avoid creation of the index column
#' @return a data frame
#' @export
#' @references Hadley Wickham (2011). The Split-Apply-Combine Strategy for
@@ -22,7 +24,7 @@
#' rdply(20, mean(runif(100)))
#' rdply(20, each(mean, var)(runif(100)))
#' rdply(20, data.frame(x = runif(2)))
-rdply <- function(.n, .expr, .progress = "none") {
+rdply <- function(.n, .expr, .progress = "none", .id = ".n") {
if (is.function(.expr)) {
f <- .expr
} else {
@@ -30,6 +32,6 @@ rdply <- function(.n, .expr, .progress = "none") {
}
res <- rlply(.n = .n, .expr = f, .progress = .progress)
- labels <- data.frame(.n = seq_len(.n))
- list_to_dataframe(res, labels)
+ names(res) <- seq_len(.n)
+ list_to_dataframe(res, idname = .id)
}
View
11 inst/tests/test-list.r
@@ -19,3 +19,14 @@ test_that("list names are preserved", {
expect_that(names(llply(a)), equals(letters[1:10]))
})
+
+# Test for #142
+test_that(".n column can be renamed", {
+ f <- function() data.frame(r=runif(1))
+
+ out1 <- rdply(4, f)
+ out2 <- rdply(4, f, .id='x')
+
+ expect_equal(names(out1), c('.n', 'r'))
+ expect_equal(names(out2), c('x', 'r'))
+})
View
6 inst/tests/test-replicate.r
@@ -7,3 +7,9 @@ test_that("length of results are correct", {
expect_equal(length(a), 4)
expect_equal(length(b), 4)
})
+
+test_that("name of id column is set", {
+ df <- rdply(4, function() c(a=1), .id='index')
+
+ expect_equal(names(df), c('index', 'a'))
+})
Please sign in to comment.
Something went wrong with that request. Please try again.