Home > other >  Looping over lists, extracting certain elements and delete the list?
Looping over lists, extracting certain elements and delete the list?

Time:01-17

I am trying to create an efficient code that opens data files containing a list, extracts one element within the list, stores it in a data frame and then deletes this object before opening the next one.

My idea is doing this using loops. Unfortunately, I am quite new in learning how to do this using loops, and don't know how write the code.

I have managed to open the data-sets using the following code:

for(i in 1995:2015){
  objects = paste("C:/Users/...",i,"agg.rda", sep=" ")
  load(objects)
} 

The problem is that each data-set is extremely large and R cannot open all of them at once. Therefore, I am now trying to extract an element within each list called: tab_<<i value >>_agg[["A"]] (for example tab_1995_agg[["A"]]), then delete the object and iterate over each i (which are different years).

I have tried using the following code but it does not work

for(i in unique(1995:2015)){
  objects = paste("C:/Users/...",i,"agg.rda", sep=" ")
  load(objects)
  tmp = cat("tab",i,"_agg[[\"A\"]]" , sep = "")
  y <- rbind(y, tmp)
  rm(list=objects)
}

I apologize for any silly mistake (or question) and greatly appreciate any help.

CodePudding user response:

Here’s a possible solution using a function to rename the object you’re loading in. I got loadRData from here. The loadRData function makes this a bit more approachable because you can load in the object with a different name.

Create some data for a reproducible example.

tab2000_agg <- 
  list(
    A = 1:5,
    b = 6:10
  )
tab2001_agg <- 
  list(
    A = 1:5,
    d = 6:10
  )

save(tab2000_agg, file = "2000_agg.rda")
save(tab2001_agg, file = "2001_agg.rda")
rm(tab2000_agg, tab2001_agg)

Using your loop idea.

loadRData <- function(fileName){
    load(fileName)
    get(ls()[ls() != "fileName"])
}

y <- list()
for(i in 2000:2001){
  objects <- paste("", i, "_agg.rda", sep="")
  data_list <- loadRData(objects)
  tmp <- data_list[["A"]]
  y[[i]] <- tmp
  rm(data_list)
}
y <- do.call(rbind, y)

You could also turn it into a function rather than use a loop.

getElement <- function(year){
  objects <- paste0("", year, "_agg.rda")
  data_list <- loadRData(objects)
  tmp <- data_list[["A"]]
  return(tmp)
}
y <- lapply(2000:2001, getElement)
y <- do.call(rbind, y)

Created on 2022-01-14 by the reprex package (v2.0.1)

  •  Tags:  
  • Related