在data.frames

时间:2015-09-14 11:54:11

标签: r reshape

我很难用简单的英语表达这一点,所以如果有人可以编辑这种语言,那将非常感激。

我有一个list对象,其中每个元素都是list data.frame个结构。

顶级列表中的某些元素可能为空,而其他元素具有不同数量的data.frames(尽管总是偶数)。

我的问题(似乎与this相反一样可疑的是:

如何在这些列表中绑定data.frames的行,以便顶级列表的每个元素包含两个数据帧?这些data.frames每次都遵循相同的结构(我想绑定data.frame数字1,3,5,7 ...的行和data.frames数字2,4,6,8 ... < / p>

MRE如下:

set.seed(1234)
listy <- list(`1` = list(),
              `2` = list(a = data.frame(a1 = runif(1:3), a2 = runif(1:3)),
                         b  = data.frame(a3 = runif(1:3), a4 = runif(1:3)),
                         c = data.frame(a1 = runif(1:3), a2 = runif(1:3)),
                         d = data.frame(a3 = runif(1:3), a4 = runif(1:3))))

listy是包含2个元素的列表(12)。 1为空的地方。 2data.frames的列表(每个都包含偶数个data.frames)。我想绑定2的行,以便顶级列表的每个元素都有2个data.frames(如果它们首先有data.frames)。

我的预期输出如下:

listb <- list(`1` = list(), 
          `2` = list(structure(list(a1 = c(0.113703411305323, 0.622299404814839, 0.609274732880294, 0.282733583590016, 0.923433484276757, 0.292315840255469), a2 = c(0.623379441676661, 0.860915383556858, 0.640310605289415, 0.837295628152788, 0.286223284667358, 0.266820780001581)), .Names = c("a1", "a2"), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")), structure(list(a3 = c(0.0094957563560456, 0.232550506014377, 0.666083758231252, 0.186722789658234, 0.232225910527632, 0.316612454829738), a3.1 = c(0.514251141343266, 0.693591291783378, 0.544974835589528, 0.302693370729685, 0.159046002896503, 0.0399959180504084)), .Names = c("a3", "a3.1"), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))))

理想情况下,我想保留listy及其结构(第一个元素为空),第二个仅保留绑定行。这就是为什么我尝试了以下内容,但无济于事:

library(dplyr)
lapply(length(listy), function(i) {
  #skip empty lists
  if(length(listy[[i]]) < 1) {
    next
  } else {
    #make two lists
    #pairs list. even numbers
    listy[[i]][[1]] <- do.call(bind_rows, listy[[i]][seq(1,length(listy[[i]]), by = 1) %% 2 == 0])
    #pairs list. odd numbers
    listy[[i]][[2]] <- do.call(bind_rows, listy[[i]][seq(1,length(listy[[i]]), by = 1) %% 2 == 1])
  }
}) 


#another try, no positive result
lapply(length(listy), function(i) {
  #skip empty lists
  if(length(listy[[i]]) < 1) {
    next
  } else {
    #make two lists
    #pairs list. even numbers
    listy[[i]][[1]] <- Reduce(bind_rows, listy[[i]][seq(1,length(listy[[i]]), by = 1) %% 2 == 0])
    #pairs list. odd numbers
    listy[[i]][[2]] <- Reduce(bind_rows, listy[[i]][seq(1,length(listy[[i]]), by = 1) %% 2 == 1])
  }
}) 

1 个答案:

答案 0 :(得分:2)

[假设列名匹配]。通常更容易对列表本身进行讨论,因为它更易于操作,而不是索引。你走了:

    listy2 <- lapply(listy, function(x){
      #get length
      current_length=length(x)
      if(current_length==0){
        res = x
      } else{
        res <- list(even=do.call(rbind,x[seq(2,current_length,by=2)]),
                    odd=do.call(rbind,x[seq(1,current_length,by=2)])
    )
        return(res)
      }
    }
    )


    > listy2
    $`1`
    list()

    $`2`
    $`2`$even
                 a3         a4
    b.1 0.009495756 0.51425114
    b.2 0.232550506 0.69359129
    b.3 0.666083758 0.54497484
    d.1 0.186722790 0.30269337
    d.2 0.232225911 0.15904600
    d.3 0.316612455 0.03999592

    $`2`$odd
               a1        a2
    a.1 0.1137034 0.6233794
    a.2 0.6222994 0.8609154
    a.3 0.6092747 0.6403106
    c.1 0.2827336 0.8372956
    c.2 0.9234335 0.2862233
    c.3 0.2923158 0.2668208


Edit with very much the same structure, but bind_rows to deal with more types inside the dataframe.

listy3 <- lapply(listy, function(x){ 
#get length 
current_length=length(x) 
if(current_length==0){ 
res = x 
} else{ 
res <- list(even=bind_rows(x[seq(2,current_length,by=2)]), 
odd=bind_rows(x[seq(1,current_length,by=2)]) 
# odd=do.call(bind_rows,x[seq(1,current_length,by=2)]) 
) 
return(res) 
} 
} 
)