R: Is there a good replacement for plyr::rbind.fill in dplyr?
Asked Answered
P

3

43

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?

Prelude answered 9/6, 2017 at 18:20 Comment(0)
P
51

Yes. dplyr::bind_rows

Credit goes to commenter.

Prelude answered 13/6, 2017 at 19:27 Comment(0)
C
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).

Curiosity answered 8/10, 2021 at 18:52 Comment(0)
M
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)
Matronna answered 13/6, 2022 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.