41

for tidyverse users, dplyr is the new way to work with data.

For users trying to avoid older package plyr, what is the equivalent function to rbind.fill in dplyr?

userJT
  • 11,486
  • 20
  • 77
  • 88

3 Answers3

49

Yes. dplyr::bind_rows

Credit goes to commenter.

userJT
  • 11,486
  • 20
  • 77
  • 88
0

An alternative:

rbindf <- function(...) {
  
  l <- list(...)
  if(length(l) == 1) l <- l[[1]]
  nn <- length(l)

  x <- l[[1]]
  if(length(l)>1){
      for(i in 2:nn) {
        y <- l[[i]]
        if(nrow(x) > 0 & nrow(y) > 0) {
          if(!all(yinx <- names(y) %in% names(x))) {
            x[, names(y)[!yinx]] <- NA
          } 
          if(!all(xiny <- names(x) %in% names(y))) {
             y[, names(x)[!xiny]] <- NA
          } 
        }
        x <- rbind(x, y)
      }
  }
  return(x)
}

From https://github.com/sashahafner/biogas (originally from https://github.com/jonkatz2/monitoR).

sashahafner
  • 435
  • 1
  • 7
0

Same Thing with R Base:

df2 <- data.frame(a = c(1:5), b = c(6:10))
df3 <- data.frame(a = c(11:15), b = c(16:20), c = LETTERS[1:5])
df2[setdiff(names(df3), names(df2))] <- NA
df3[setdiff(names(df2), names(df3))] <- NA

rbind(df2, df3)
rubengavidia0x
  • 501
  • 1
  • 5
  • 18