函数绑定不同列和行的数据框列表

时间:2019-11-08 18:17:06

标签: r data.table rbind

我想创建一个函数,该函数合并具有不同列号的数据帧列表,并且行具有我想保留的不同名称。本质上,我想堆叠其中列名仅成为要附加的另一行的数据框。

df <- list()

df[[1]] <-  data.frame(d = c(4,5), e = c("c", "d"))
rownames(df[[1]]) <- c("df2_row_1", "df2_row_2")

df[[2]] <- data.frame(a = c(1,2,3), b = c("a", "b", "c"), c = c("one", "two", "three"))
rownames(df[[2]]) <- c("df1_row_1", "df1_row_2", "df1_row_3")


df[[3]] <- data.frame(f = c(6,7,8), g = c("e", "f", "g"), h = c("one", "two", "three"), w = c(100,101,102))
rownames(df[[3]]) <- c("df3_row_1", "df3_row_2", "df3_row_3")

电流输出:

do.call(bind_rows, df)

   d    e  a    b     c  f    g     h   w
1  4    c NA <NA>  <NA> NA <NA>  <NA>  NA
2  5    d NA <NA>  <NA> NA <NA>  <NA>  NA
3 NA <NA>  1    a   one NA <NA>  <NA>  NA
4 NA <NA>  2    b   two NA <NA>  <NA>  NA
5 NA <NA>  3    c three NA <NA>  <NA>  NA
6 NA <NA> NA <NA>  <NA>  6    e   one 100
7 NA <NA> NA <NA>  <NA>  7    f   two 101
8 NA <NA> NA <NA>  <NA>  8    g three 102

所需的输出

          d e  
df2_row_1 4 c 
df2_row_2 5 d
          a b     c 
df1_row_1 1 a   one 
df1_row_2 2 b   two 
df1_row_3 3 c three 
          f g     h   w
df3_row_1 6 e   one 100
df3_row_2 7 f   two 101
df3_row_3 8 g three 102

我尝试过(未成功)创建一个函数,该函数查找最长的数据帧,然后将空列添加到比最长的数据帧短的数据帧中,然后为所有数据帧赋予相同的名称。

我也意识到这不是更加整洁-这可能吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

这可以通过for循环来实现(我认为可以通过mapply至,检查?mapply来实现)。总体策略是用NA填充列表中的每个df(cbind填充它们),然后rbindlist填充结果列表:

library(data.table)

cols <- max(sapply(df, ncol))

# This is the length of the NA vectors that make the cbinding dfs:
lengths <- (cols - sapply(df, ncol))*sapply(df, nrow)

newdf <- list()

for (i in 1:length(df)){
  if (ncol(df[[i]]) != cols){
    newdf[[i]] <- cbind(df[[i]], 
                        as.data.frame(matrix(rep(NA, lengths[i]), 
                                             ncol = lengths[i] / nrow(df[[i]]))))
  } else {
    newdf[[i]] <- df[[i]]
  }
}

rbindlist(newdf, use.names = FALSE)

这将导致:

   d e    V1  V2
1: 4 c  <NA>  NA
2: 5 d  <NA>  NA
3: 1 a   one  NA
4: 2 b   two  NA
5: 3 c three  NA
6: 6 e   one 100
7: 7 f   two 101
8: 8 g three 102