Permalink
Browse files

Fix bug exposed by plyr

  • Loading branch information...
hadley committed Feb 27, 2012
1 parent 63e56c5 commit b9472b7f137df2d44cc07307079d0959e0eedefb
Showing with 15 additions and 5 deletions.
  1. +1 −1 DESCRIPTION
  2. +6 −0 NEWS
  3. +1 −1 R/helper-margins.r
  4. +7 −3 inst/tests/test-margins.r
View
@@ -2,7 +2,7 @@ Package: reshape2
Type: Package
Title: Flexibly reshape data: a reboot of the reshape
package.
-Version: 1.2
+Version: 1.2.1
Author: Hadley Wickham <hadley@rice.edu>
Maintainer: Hadley Wickham <hadley@rice.edu>
Description: Reshape lets you flexibly restructure and
View
6 NEWS
@@ -1,3 +1,9 @@
+Version 1.2.1
+-------------
+
+* Fix bug in multiple margins revealed by plyr 1.7, but caused by mis-use of
+ data frame subsetting.
+
Version 1.2
-----------
View
@@ -75,7 +75,7 @@ add_margins <- function(df, vars, margins = TRUE) {
# Loop through all combinations of margin variables, setting
# those variables to (all)
margin_dfs <- llply(margin_vars, function(vars) {
- df[vars] <- factor("(all)")
+ df[vars] <- rep(list(factor("(all)")), length(vars))
df
})
View
@@ -18,13 +18,17 @@ test_that("margins intersect", {
})
test_that("(all) comes after NA", {
- df <- data.frame(a = c("a", "b", NA), value = 1)
+ df <- data.frame(a = c("a", "b", NA), b = c("a", "b", NA), value = 1)
df2 <- add_margins(df, "a")
expect_that(levels(df2$a), equals(c("a", "b", NA, "(all)")))
-
+
+ df3 <- add_margins(df, c("a", "b"))
+ expect_that(levels(df3$a), equals(c("a", "b", NA, "(all)")))
+ expect_that(levels(df3$b), equals(c("a", "b", NA, "(all)")))
+
dc <- dcast(df, a ~ ., margins = TRUE, fun = length)
expect_that(levels(dc$a), equals(c("a", "b", NA, "(all)")))
expect_that(as.character(dc$a), equals(c("a", "b", NA, "(all)")))
-})
+})

0 comments on commit b9472b7

Please sign in to comment.