0

I want to create dataframes in a for loop where every dataframe gets a value specified in a vector. It seems very simple but for some reason I cannot find the answer.

So what I want is something like this:

x <- c(1,2,3)

for (i in x) {
  df_{{i}} <- ""
  return df_i
}

The result I want is:

df_1
df_2
df_3

So df_{{i}} should be something else but I don't know what.

EDIT: I have solved my problem by creating a list of lists like this:

function_that_creates_model_output <- function(var) {
output_function <- list()
output_function$a <- df_a %>% something(var)
output_function$b <- df_b %>% something(var)
return(output_function)
}

meta_output <- list()
for (i in x) {
meta_output[[i]] <- function_that_creates_model_output(var = i)
}
Nina van Bruggen
  • 393
  • 2
  • 13
  • 1
    Does this answer your question? [How do I create an object name in R](https://stackoverflow.com/questions/25222187/how-do-i-create-an-object-name-in-r) – starja Sep 01 '21 at 14:27
  • 7
    If you are creating multiple similar frames (and not just `""`), then perhaps they should be stored as a [list of frames](https://stackoverflow.com/a/24376207/3358227). The rationale is that what you do to one frame is likely to be done to all of them, in which case `lapply` is a **great** way to do things. Outside of that, the way to create individual objects programmatically is via [`assign`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/assign.html), though most "best practices" say that its use is generally a sign of poor software design. – r2evans Sep 01 '21 at 14:27
  • 2
    The answer is: **don’t**. Instead, store the data frames *In a list*. – Konrad Rudolph Sep 01 '21 at 14:30
  • Thanks for your comment. What I actually want is assign a name to the model object that I return from my function. So it's one of the parameters of the function that creates a model. So the models would be different from each other. – Nina van Bruggen Sep 01 '21 at 14:32
  • 1
    Unfortunately the question really isn’t answerable in its current form because you haven’t provided enough information about what you actually want to do. You say that you want to create data frames, but your code contains no data frames — you’re assigning empty strings. Where is your data coming from? Can you simply `rbind` all the data together, for example? – Konrad Rudolph Sep 01 '21 at 14:40
  • Thanks all for your help, I have edited the question with my solution. – Nina van Bruggen Sep 07 '21 at 14:12

1 Answers1

0

One solution would be to use the function assign

x <- c(1,2,3)

for (i in x) {
  assign(x = paste0("df_",i),value = NULL)
}

enter image description here

Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32
  • 1
    @KonradRudolph care to explain? – denisafonin Sep 01 '21 at 14:36
  • 4
    @denisafonin It abuses a function which wasn’t designed for this purpose, clutters the local environment, is overly verbose, makes the code harder to reason about, is less efficient and, lastly, does nothing useful (it creates empty variables; might as well not have created them!). — The correct solution is to use a list, as noted in the comments below the question. There are other solutions which might be more appropriate (reshaping the data into a shared table is a common pattern) but the solution suggested here is pretty much *never* appropriate. – Konrad Rudolph Sep 01 '21 at 14:38