0

I have a model object m1. I need to create 100 distinctly named copies so I can adjust and plot each. To create a copy, I currently do this as such:

m1recip1 <- m1
m1recip2 <- m1
m1recip3 <- m1
m1recip4 <- m1
m1recip5 <- m1
m1recip6 <- m1
m1recip7 <- m1
...
m1recip100 <- m1

I planned to create these through a loop, but this is less efficient because I only know how to do so by initializing all 100 objects before looping through them. I'm effectively looking for something similar to the macro facility in other languages (where m1recip&i would produce the names iteratively). I'm sure R can do this - how?

7
  • 1
    Spawning objects in a loop is usually considered bad practice. Try using built-in R data structures made specifically for this purpose, specifically, data frames Commented Nov 15, 2018 at 19:09
  • Like this? stackoverflow.com/questions/7914568/r-create-variables-in-loop Commented Nov 15, 2018 at 19:09
  • 1
    Instead of creating 100 objects in your environment, create a list with 100 elements like: m1recip_list <- lapply(1:100, function(x) m1) Commented Nov 15, 2018 at 19:11
  • 3
    If you go this route, please try to be aware that any frustration you encounter about "how hard this seems to be in R compared to other languages" is likely because this sort of thing is generally done much more efficiently using other data structures (lists) or a function. Commented Nov 15, 2018 at 19:11
  • @user3470883 not really - those variables already exist, so the winning solution doesn't get me there (mget() breaks because the var_names made through paste don't exist to be put into a list...)...but it looks like some related suggestions are getting me there. Thanks! Commented Nov 15, 2018 at 19:22

3 Answers 3

3

As mentioned above, reconsider saving many similar structured objects in global environment. Instead, use a named list which results in the maintenance of one, indexed object to maintain where R has many handlers (i.e., apply family) to run operations across all elements.

Specifically, consider replicate (wrapper to sapply) to build the 100 m1 elements and use setNames to name them accordingly. You lose no functionality of object if saved within a list.

model_list <- setNames(replicate(100, m1, simplify = FALSE),
                       paste0("m1recip", 1:100))

model_list$m1recip1
model_list$m1recip2
model_list$m1recip3
...
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for making the reasoning behind this so clear
1

Instead of assigning m1 to 100 objects, we can create a list with 100 elements like the following:

m1recip_list <- lapply(1:100, function(x) m1)

We can then reference each element by element number m1recip_list[[10]] or apply a function to every element of the list using lapply:

lapply(m1recip_list, some_function)

Comments

0

You can dynamically create object names using the paste function in a loop, and you can assign them values using the assign function as opposed to the "<-" operator.

for(i in 1:100) {
  assign(paste("m1recip",i, sep = ""), m1)
}

1 Comment

You can, but you shouldn't.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.