Tom
Tom

Reputation: 199

R repetitive subset of lists nested within multiple lists

I have a data set that was supplied to me in .xlsx form with multiple workbooks with multiple sheets with the same names and similar columns of data within those sheets. I want to eventually join the data sheets with the same names and run some analysis.

As there is a lot of data in workbooks from multiple years I want to efficiently join the data and format, analysis etc... I am new to lists but have successfully read the data in but am having trouble sub setting and binding. I have made the following dummy data and script:

#tlm2009
b1<-c('a', 'b', 'c')
b2<-    c(4,    2,  3)
hab<-data.frame(b1,b2)

c1<-c('a', 'b', 'c', 'd')
c2<-    c(1,    2,  3, 4)
raw<-data.frame(c1,c2)

tlm2009<-list(hab,raw)

#tlm2010

b1<-c('a', 'b', 'c','d')
b2<-    c(1,    2,  3, 4)
hab<-data.frame(b1,b2)

c1<-c('a', 'b', 'c', 'd', "e")
c2<-    c(1,    2,  3, 4, 5)
raw<-data.frame(c1,c2)

tlm2010<-list(hab,raw)

my.list<-list(tlm2009,tlm2010)

I have then run the following code to extract 'hab' and or 'raw' but it is not working

library(tidyverse)
unlist(my.list) %>%
    data.frame(val = .) %>%
    filter(str_detect(id, "(hab|raw)"))

and

library(purrr)
map(transpose(my.list),~map_dfc(.x,"hab"))

and get the following error for the transpose:

Error in transpose(my.list) : Item 1 of list input is not an atomic vector

So I would like all the 'habs' subset and joined, I haven't really contemplated the joining yet as haven't got over the subset. Any help would be appreciated.

Upvotes: 1

Views: 593

Answers (2)

akrun
akrun

Reputation: 887501

A general option would be to use do.call with Map. It can be also take lists of multiple lengths

do.call(Map, c(f = rbind, my.list))
#[[1]]
#  b1 b2
#1  a  4
#2  b  2
#3  c  3
#4  a  1
#5  b  2
#6  c  3
#7  d  4

#[[2]]
#  c1 c2
#1  a  1
#2  b  2
#3  c  3
#4  d  4
#5  a  1
#6  b  2
#7  c  3
#8  d  4
#9  e  5

Or using tidyverse

library(tidyverse)
pmap(my.list, bind_rows)

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389125

Maybe you are looking for something like this

Map(rbind, my.list[[1]], my.list[[2]])

#[[1]]
#  b1 b2
#1  a  4
#2  b  2
#3  c  3
#4  a  1
#5  b  2
#6  c  3
#7  d  4

#[[2]]
#  c1 c2
#1  a  1
#2  b  2
#3  c  3
#4  d  4
#5  a  1
#6  b  2
#7  c  3
#8  d  4
#9  e  5

This automatically brings your hab and raw datasets together in one list by rbinding them.

Upvotes: 2

Related Questions