Solebay Sharp
Solebay Sharp

Reputation: 533

How to create complex hierarchies of lists in R

I have received help on how to create a set of lists within lists, however I have been unable to add another layer / extend the depth of my lists. All I want is to add a final 'layer' in each list, that reads, 'DataFrame', 'DataFrame2', and so on. Currently I have:

Layer1 = c('AA', 'BB', 'CC', 'DD')

myList=setNames(as.list(Layer1),Layer1)

myList=lapply(myList, function(x){
  setNames(vector("list",length(Layer1)),paste0(x," vs ",Layer1))
})

Which produces myList, containing AA, BB, CC and DD, in each of those lists is a further list e.g. AA vs BB, AA vs BB etc, or in the case of BB the lists inside will read BB vs AA, BB vs BB (referred to hereafter as ?? vs ?? files) and so on and so forth.

So I thought I could easily add an additional layer to this by doing something along the lines of...

Layer1 = c('AA', 'BB', 'CC', 'DD')
Layer3 = c('DataFrame', 'DataFrame2', 'Matrix', 'Matrix2')

myList=setNames(as.list(Layer1),Layer1)

myList=lapply(myList, function(x){
  setNames(vector("list",length(Layer1)),paste0(x," vs ",Layer1))

  myList[i]=lapply(myList, function(x){
  setNames(vector("list",length(Layer3)),Layer3)

  })  
})

Where I've naively tried to use myList[i] (which I know won't work, but I'm not sure if anything I'm doing will) to indicate that I'd like to move down a tier and start adding blank DataFrame and Matrix vectors (into my ?? vs ?? sublists) so that I have 'empty slots'- so to speak - to move my data into in the future.

Ultimately I would like each ?? vs ?? folder to contain a blank DataFrame, DataFrame2, Matrix, Matrix2.

Upvotes: 1

Views: 40

Answers (1)

thothal
thothal

Reputation: 20329

lapply loops over each element of a list like structure and applies a function to it. Notably, it does not include a positional argument.

What you want to do is:

  • run through all elements of Layer1 and create for each element a list, which in turn
  • contains Layer1 many elements wach of these elements contain
  • as many elements as given in Layer3

Code

Layer1 <- c('AA', 'BB', 'CC', 'DD')
Layer3 <- c('DataFrame', 'DataFrame2', 'Matrix', 'Matrix2')

my_list <- lapply(Layer1, function(el_layer1_outer) {
   ## create a list with |Layer1| elements
   ## this we do by creating first an inner list vector(.)
   ## and the repeating it |Layer1| times
   ret <- rep(list(setNames(vector("list", length(Layer3)), 
                            Layer3)), 
              length(Layer1))
   setNames(ret, ## ret has no proper names yet
            paste(el_layer1_outer, "vs.", Layer1)) 
})
names(my_list) <- Layer1 ## could have been done with setNames as well
str(my_list)
List of 4
 $ AA:List of 4
  ..$ AA vs. AA:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ AA vs. BB:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ AA vs. CC:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ AA vs. DD:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
 $ BB:List of 4
  ..$ BB vs. AA:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ BB vs. BB:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ BB vs. CC:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ BB vs. DD:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
 $ CC:List of 4
  ..$ CC vs. AA:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ CC vs. BB:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ CC vs. CC:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ CC vs. DD:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
 $ DD:List of 4
  ..$ DD vs. AA:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ DD vs. BB:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ DD vs. CC:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ DD vs. DD:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL

Upvotes: 1

Related Questions