将数据帧彼此堆叠,然后返回一个数据帧

时间:2019-06-23 05:14:02

标签: r

我正在尝试编写一个函数(stackDataInList)将数据帧堆叠在一起,但是这样做很麻烦,非常感谢您的帮助

>datList
[[1]]
ID value
1 a 0.9101742
2 b 0.3841854
3 c 1.6821761
[[2]]
ID value
1 d -0.6357365
2 e -0.4616447
[[3]]
ID value
1 f 1.4322822
2 g -0.6506964
3 h -0.2073807
4 i -0.3928079
[[4]]
ID value
1 j -0.3199929
2 k -0.2791133
3 l 0.4941883

dataList看起来像它

所以基本上我需要创建一个函数,当我调用它时,我将在下面得到结果:

>stackDataInList(datList[1])
ID value
1 a 0.9101742
2 b 0.3841854
3 c 1.6821761
OR:
>stackDataInList(datList[c(1,3,4)])
ID value
1 a 0.9101742
2 b 0.3841854
3 c 1.6821761
4 f 1.4322822
5 g -0.6506964
6 h -0.2073807
7 i -0.3928079
8 j -0.3199929
9 k -0.2791133
10 l 0.4941883

2 个答案:

答案 0 :(得分:2)

您可以通过多种方法来执行此操作。最快的方法(尤其是大数据帧)是对rbindlist()包使用data.table函数:

library(data.table)
datList <- list( data.frame(ID=c("a","b","c"), value=c(0.9101742,0.3841854, 1.6821761)),
             data.frame(ID=c("d","e"), value=c(-0.6357365,-0.4616447)),
             data.frame(ID=c("f","g","e"), value=c(1.4322822,-0.6506964,-0.2073807)))


rbindlist(datList)
#   ID      value
#1:  a  0.9101742
#2:  b  0.3841854
#3:  c  1.6821761
#4:  d -0.6357365
#5:  e -0.4616447
...

或对列表进行分组:

rbindlist(datList[c(1,3)])
#    ID      value
# 1:  a  0.9101742
# 2:  b  0.3841854
# 3:  c  1.6821761
# 4:  f  1.4322822
# 5:  g -0.6506964
# 6:  e -0.2073807

答案 1 :(得分:1)

正如 @PKumar 在评论中指出的那样,在底数R中,我们可以使用do.call(rbind)。只需将其放入函数中即可。

stackDataInList <- function(x) {
  return(do.call(rbind, x))
}

stackDataInList(L[1])  
#   ID     value
# 1  a 0.9101742
# 2  b 0.3841854
# 3  c 1.6821761

stackDataInList(L[c(1, 3, 4)])  
#    ID      value
# 1   a  0.9101742
# 2   b  0.3841854
# 3   c  1.6821761
# 4   f  1.4322822
# 5   g -0.6506964
# 6   h -0.2073807
# 7   i -0.3928079
# 8   j -0.3199929
# 9   k -0.2791133
# 10  l  0.4941883

或使用stackmapply

stackDataInList2 <- function(x) setNames(cbind.data.frame(stack(mapply(`[`, x, 1))[[1]],
                                                          stack(mapply(`[`, x, 2))[[1]]), 
                                         names(x[[1]]))

stackDataInList2(L[1])  
# ID     value
# 1  a 0.9101742
# 2  b 0.3841854
# 3  c 1.6821761
stackDataInList2(L[c(1, 3, 4)])  
# ID      value
# 1   a  0.9101742
# 2   b  0.3841854
# 3   c  1.6821761
# 4   f  1.4322822
# 5   g -0.6506964
# 6   h -0.2073807
# 7   i -0.3928079
# 8   j -0.3199929
# 9   k -0.2791133
# 10  l  0.4941883

Reduce也是一个选项,找到here

stackDataInList3 <- function(x) Reduce(rbind, x)
stackDataInList3(L[1])  
#   ID     value
# 1  a 0.9101742
# 2  b 0.3841854
# 3  c 1.6821761
stackDataInList3(L[c(1, 3, 4)]) 
#    ID      value
# 1   a  0.9101742
# 2   b  0.3841854
# 3   c  1.6821761
# 4   f  1.4322822
# 5   g -0.6506964
# 6   h -0.2073807
# 7   i -0.3928079
# 8   j -0.3199929
# 9   k -0.2791133
# 10  l  0.4941883

数据

L <- list(structure(list(ID = c("a", "b", "c"), value = c(0.9101742, 
0.3841854, 1.6821761)), row.names = c(NA, -3L), class = "data.frame"), 
    structure(list(ID = c("d", "e"), value = c(-0.6357365, -0.4616447
    )), row.names = c(NA, -2L), class = "data.frame"), structure(list(
        ID = c("f", "g", "h", "i"), value = c(1.4322822, -0.6506964, 
        -0.2073807, -0.3928079)), row.names = c(NA, -4L), class = "data.frame"), 
    structure(list(ID = c("j", "k", "l"), value = c(-0.3199929, 
    -0.2791133, 0.4941883)), row.names = c(NA, -3L), class = "data.frame"))