Permalink
Browse files

Generate .id column as factor in ldply(); fixes # 137

  • Loading branch information...
1 parent 940407d commit 1962bd06f564b062434cecd3869a63b54a75f256 @krlmlr krlmlr committed with krlmlr Mar 22, 2013
Showing with 4 additions and 4 deletions.
  1. +1 −1 R/list-to-dataframe.r
  2. +3 −3 inst/tests/test-simplify-df.r
View
@@ -22,7 +22,7 @@ list_to_dataframe <- function(res, labels = NULL, idname = NULL) {
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)
+ labels <- data.frame(.id = factor(names.res, levels = unique(names.res)))
names(labels) <- idname
}
@@ -103,15 +103,15 @@ test_that("names preserved and filled for atomic vectors", {
})
test_that("names captured from list", {
- li <- list(a = 1:5, b = 5:10, c = 5:15)
+ li <- list(c = 5:15, b = 5:10, a = 1:5)
df <- ldply(li, function(x) mean(x))
- expect_that(df$.id, equals(c("a", "b", "c")))
+ expect_that(df$.id, equals(factor(names(li), levels=names(li))))
df <- ldply(li, function(x) {
if (any(x >= 10)) mean(x)
})
- expect_that(df$.id, equals(c("b", "c")))
+ expect_that(df$.id, equals(factor(names(li)[-3], levels=names(li)[-3])))
})
test_that("correct number of rows outputted", {

0 comments on commit 1962bd0

Please sign in to comment.