Permalink
Browse files

rbind.fill respects matrix column dimnames

  • Loading branch information...
1 parent 1058ae8 commit 0f629ef2298b0eed298707472b67776fc23ca060 @crowding crowding committed Sep 1, 2013
Showing with 25 additions and 3 deletions.
  1. +3 −2 NEWS
  2. +5 −1 R/rbind-fill.r
  3. +17 −0 inst/tests/test-rbind.r
View
5 NEWS
@@ -6,8 +6,9 @@ Version 1.8.0.99
* `rbind.fill` handles non-numeric matrix columns (i.e. factor arrays,
character arrays, list arrays); also arrays with more than 2
- dimensions can be used. (Contributed by Peter Meilstrup)
-
+ dimensions can be used. Dimnames of array columns are now preserved.
+ (Contributed by Peter Meilstrup)
+
* `rbind.fill(x,y)` converts factor columns of Y to character when
columns of X are character. `join(x,y)` and `match_df(x,y)` now work
when the key column in X is character and Y is factor. (Contributed
View
@@ -109,7 +109,11 @@ allocate_column <- function(example, nrows, dfs, var) {
if (is.array(example)) {
- a$dimnames <- NULL #todo: check if rbind handles these
+ if ("dimnames" %in% names(a)) {
+ a$dimnames[1] <- list(NULL)
+ if (!is.null(names(a$dimnames)))
+ names(a$dimnames)[1] <- NA
+ }
# Check that all other args have consistent dims
df_has <- vapply(dfs, function(df) var %in% names(df), FALSE)
View
@@ -122,6 +122,23 @@ test_that("multidim arrays ok", {
expect_that(df2$x, is_equivalent_to(abind(along=1, df$x, df$x)))
})
+test_that("Array column names preserved", {
+ x <- data.frame(hair.color=dimnames(HairEyeColor)[[1]])
+ x$obs <- HairEyeColor[,,1]
+
+ xx1 <- rbind(x, x)
+ xx2 <- rbind.fill(x, x)
+
+ #plyr is against row names, but should respect col names like rbind
+ rownames(xx1) <- NULL
+ rownames(xx1$obs) <- NULL
+
+ #but unlike rbind it should also preserve names-of-dimnames.
+ names(dimnames(xx1$obs)) <- c(NA, "Eye")
+
+ expect_equal(xx1, xx2)
+})
+
test_that("attributes are preserved", {
d1 <- data.frame(a = runif(10), b = runif(10))
d2 <- data.frame(a = runif(10), b = runif(10))

0 comments on commit 0f629ef

Please sign in to comment.