Search code examples
rlist

How to create a list whose name depends on loop index in a for loop in R?


I would like to create several lists within a for loop and each list should include the loop index in its name. I need to do it because otherwise the list object is apparently too big (>20GB) [derived from 1000 random replicates of 9000 events] and I cannot export it, neither using save.image() nor saveRDS().

Here is a small example of the whole procedure, with a single list for all objects:

library(tidyverse)

SO_ListIteration <- data.frame(Group = rep(1:3, each = 20),
                               Replicate = rep(1:10, each = 2),
                               Data = rnorm(60))


SO_ListIteration_ForEachGroup <- list()
for (j in 1:max(SO_ListIteration$Group)) {
  SO_ListIteration_ForEachGroup[[j]] <- list()
  temp_grp <- SO_ListIteration %>%
    filter(Group == j)
  
  for (i in 1:max(SO_ListIteration$Replicate)) {
    temp <- temp_grp %>%
      filter(Replicate == i)

    SO_ListIteration_ForEachGroup[[j]][[i]] <- temp
    rm(temp)
  }
  rm(temp_grp)
}

Each element of the list contains the data for a replicate of a group, but I would like to have one list per group.

When I try the following code, I obviously only get the data for the last group, not for the other two:

SO_ListIteration_ForEachGroup <- list()
for (j in 1:max(SO_ListIteration$Group)) {
  temp_grp <- SO_ListIteration %>%
    filter(Group == j)
  
  for (i in 1:max(SO_ListIteration$Replicate)) {
    temp <- temp_grp %>%
      filter(Replicate == i)
    
    SO_ListIteration_ForEachGroup[[i]] <- temp
    rm(temp)
  }
  rm(temp_grp)
}

I tried apparently silly things based on paste0(), such as paste0("SO_ListIteration_ForGroup", j) <- list(), but then I get Error in paste0("SO_ListIteration_ForGroup", j) <- list() : target of assignment expands to non-language object

I thought it would be easy, but could not find a solution so far.


Solution

  • I am not sure what the the disered output is supposed to look like. You could try something like this:

    SO_ListIteration <- data.frame(Group = rep(1:3, each = 20),
                                   Replicate = rep(1:10, each = 2),
                                   Data = rnorm(60))
    
    
    for(j in 1:max(SO_ListIteration$Group)){
    
        temp_gr <- SO_ListIteration[SO_ListIteration$Group==j,]
        assign(paste0("loop",j),split(temp_gr,temp_gr$Replicate))
    
    }
    

    This gets you three lists (loop1, loop2, loop3) carrying the replicates of the respective group.

    loop1
    
    > loop1
    $`1`
      Group Replicate       Data
    1     1         1 -1.3843642
    2     1         1 -0.8653507
    
    $`2`
      Group Replicate      Data
    3     1         2 0.8221535
    4     1         2 0.3215665
    
    $`3`
      Group Replicate       Data
    5     1         3 -0.2025913
    6     1         3 -1.0237386
    
    $`4`
      Group Replicate      Data
    7     1         4  1.769719
    8     1         4 -1.431969
    
    $`5`
       Group Replicate       Data
    9      1         5  0.6306618
    10     1         5 -0.1247508
    
    $`6`
       Group Replicate      Data
    11     1         6 -2.128931
    12     1         6 -1.512757
    
    $`7`
       Group Replicate       Data
    13     1         7 -0.8367149
    14     1         7  0.4827991
    
    $`8`
       Group Replicate      Data
    15     1         8 0.4900297
    16     1         8 2.1262468
    
    $`9`
       Group Replicate       Data
    17     1         9 0.49559605
    18     1         9 0.02258138
    
    $`10`
       Group Replicate       Data
    19     1        10 0.04831295
    20     1        10 1.75682687