Permalink
Browse files

bug fix

  • Loading branch information...
hadley committed Apr 15, 2009
1 parent 83db497 commit 2b919569412b7285bff5743ceecd89e5722c446a
Showing with 29 additions and 9 deletions.
  1. +1 −1 DESCRIPTION
  2. +5 −1 NEWS
  3. +23 −7 R/rbind.r
View
@@ -1,7 +1,7 @@
Package: plyr
Type: Package
Title: Tools for splitting, applying and combining data
Version: 0.1.6
Version: 0.1.7
Author: Hadley Wickham <h.wickham@gmail.com>
Maintainer: Hadley Wickham <h.wickham@gmail.com>
Description: plyr is a set of tools that solves a common set of problems: you need to break a big problem down into manageable pieces, operate on each pieces and then put all the pieces back together. For example, you might want to fit a model to each spatial location or time point in your study, summarise data by panels or collapse high-dimensional arrays to simpler summary statistics.
View
6 NEWS
@@ -1,4 +1,8 @@
plyr 0.1.6 (2008-XX-XX) ---------------------------------------------------
plyr 0.1.7 (2008-04-15) ---------------------------------------------------
Ensure that rbind.fill copies attributes.
plyr 0.1.6 (2008-04-15) ---------------------------------------------------
Improvements:
View
@@ -13,6 +13,15 @@
#X rownames(b1) <- NULL
#X system.time(b2 <- rbind.fill(bplayer))
#X stopifnot(all.equal(b1, b2))
#X
#X a <- data.frame(a = factor(letters[1:3]), b = 1:3, c = date())
#X b <- data.frame(a = factor(letters[3:5]),
#X d = as.Date(c("2008-01-01", "2009-01-01", "2010-01-01")))
#X ab1 <- rbind.fill(a, b)[, letters[1:4]]
#X ab2 <- rbind.fill(b, a)[c(4:6, 1:3), letters[1:4]]
#X ab2$a <- factor(ab2$a, levels(ab1$a))
#X rownames(ab2) <- NULL
#X stopifnot(all.equal(ab1, ab2))
rbind.fill <- function(...) {
dfs <- list(...)
if (length(dfs) == 0) return(list())
@@ -21,14 +30,18 @@ rbind.fill <- function(...) {
}
rows <- unlist(lapply(dfs, nrow))
nrows <- sum(rows)
output <- list()
seen <- character()
# Set up factors
factors <- names(dfs[[1]])[laply(dfs[[1]], is.factor)]
for(var in factors) {
all <- llply(dfs, function(df) levels(df[[var]]))
output[[var]] <- factor(levels = unique(unlist(all)))
length(output[[var]]) <- nrows
seen <- c(seen, var)
}
# Compute start and end positions for each matrix
@@ -40,16 +53,19 @@ rbind.fill <- function(...) {
df <- dfs[[i]]
for(var in names(df)) {
if (length(df[[var]]) > 0) output[[var]][rng] <- df[[var]]
if (length(df[[var]]) > 0) {
output[[var]][rng] <- df[[var]]
if (!var %in% seen) {
length(output[[var]]) <- nrows
attributes(output[[var]]) <- attributes(df[[var]])
seen <- c(seen, var)
}
}
}
}
# Ensure all variables are the same length. They might not be if the
# last data frame does not contain all rows.
for(var in names(output)) {
length(output[[var]]) <- sum(rows)
}
as_df(output)
}

0 comments on commit 2b91956

Please sign in to comment.