1

I have 5 R objects with different names to load, each one contains a list with the same name. Is there any way that I can load them and change the list name at the same time? (Or other ways more efficient than what I do below?)

R objects name: A, B, C, D, E

List in the R object name: result

List names to change to: A_result, B_result, C_result, D_result, E_result

How I do it now:

load_name <- c('A','B','C','D','E')

for (i in 1:5){
    load (paste0 ('Output/Compiled_Models/',load_name[[i]]))
  }

A_result <- A
B_result <- B
C_result <- C
D_result <- D
E_result <- E

rm (list = A)
rm (list = B)
rm (list = C)
rm (list = D)
rm (list = E)
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Kenny
  • 361
  • 1
  • 8
  • 1
    Doing it this way is fundamentally not a good idea: You should consider varible names as *static* and unchangeable. I suggest you change your workflow, starting by not using `load` and `save`, and instead using `readRDS` and `saveRDS`. Then, instead of creating and renaming objects, load objects *into a named list*. – Konrad Rudolph Jan 15 '22 at 14:32

1 Answers1

1

It is not entirely clear what object you work with. Indeed, it is preferable to work with RDS instead of .RData. You are using save and load, so I've written a small reproducible example to get you going.

A <- lm(mpg ~ ., data = mtcars)
B <- lm(disp ~ ., data = mtcars)

save(A, file = "A.RData")
save(B, file = "B.RData")


load_name <- c('A','B')
read_name <- paste0("read_", load_name)

for (i in seq_along(load_name)){
  x <- load(paste0 (load_name[i], ".RData"))
  assign(read_name[i], get(x))
  rm(list = x, envir = .GlobalEnv)
}
Claudiu Papasteri
  • 2,469
  • 1
  • 17
  • 30
  • 1
    Alternatively, you can just loop using ricardo's function from here: https://stackoverflow.com/questions/5577221/how-can-i-load-an-object-into-a-variable-name-that-i-specify-from-an-r-data-file – Claudiu Papasteri Jan 15 '22 at 14:32
  • 3
    This narrowly solves OP’s exact requirement but *it’s not a good solution*, because OP’s requirements are an X-Y problem. I strongly believe that, before giving beginners solutions that contain ill-advised code such as `assign`, it behooves us to explain that this is bad code. You do briefly mention a better solution via RDS but you don’t explain that using `assign` is bad. – Konrad Rudolph Jan 15 '22 at 14:34
  • 1
    Well, you can write it in many ways and I supposed OP wants a simple answer. I also suggested looping over ricardo's `loadRData` function which would be simple and would not require `assign`. Nonetheless, he has to get the job done and probably can't go back to do it 'the correct way'. – Claudiu Papasteri Jan 15 '22 at 14:43
  • 1
    @KonradRudolph @ClaudiuPapasteri, thank you so much for both of your answers. I have done the current analysis with 'looping assign' and at the same time changing my workflow to `RDS` (loading all `RDS` files and assign names in a list). May I also ask why `assign` is an ill-advised code? – Kenny Jan 16 '22 at 15:25
  • @Shepard From my modest point of view there is nothing inherently wrong with `assign` or `<<-` but they are meant to be used with environments, not to litter the global env with variables like I did in the code example. @KonaradRudolph is right that using named lists is the true R way. Read more here: https://stackoverflow.com/questions/17559390/why-is-using-assign-bad – Claudiu Papasteri Jan 16 '22 at 16:02
  • @Shepard The thing that’s wrong with it is that it makes your code more complex for no gain, for two reasons. The first reason is that it literally leads you to write more, and more complex, code than using e.g. lists. And the second is that, once you use it, variables are no longer static. That is, you can no longer keep track of what the variables in your code are simply by looking at the code. Variables are now created dynamically. This makes the code a lot harder to understand, and makes bugs harder to diagnose. – Konrad Rudolph Jan 16 '22 at 17:22