It has been hard for me expressing this in plain English, so if anyone can edit the language, that would be greatly appreciated.
I have a list object, where every element is a list of data.frame structures.
Some of these elements in the top level list might be empty, while the others have different numbers of data.frames (always an even number though).
My question (which seems suspiciously like the opposite of this one, is this:
How can I bind the rows of the data.frames in these lists so every element of the top-level list contains two data frames? These data.frames follow the same structure every time (I want to bind the rows of the data.frame number 1,3,5,7... and the data.frames number 2,4,6,8...
MRE as follows:
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 is a list of 2 elements (1,2). Where 1 is empty. 2 is a list of data.frames (each with an even number of data.frames). I want to bind the rows of 2 so every element of the top level list has 2 data.frames (if they had data.frames in the first place).
My expected output is the following:
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"))))
Ideally, I would like to preserve listy, and its structure (the first element empty), the second one just with the bound rows. That is why I have tried the following, with no avail:
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])
}
})
[Assuming that column names match]. It's often easier to lapply over the list itself because it gets easier to manipulate, instead of indices. Here you go:
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)
}
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With