Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create list of variables based on names of variable

I’m running a loop that creates a large number (n) of dataframes called df-1 , df-2, df-3 up to df-n.

Next, I would like to create a list that contains all these dataframes:

listofdf = list(df-1, df-2, df-3, ... df-n)

I suppose I need a function that creates a list of all objects whose name contains "df-", this list is then input into the list() function. I’m stuck on the first part, though.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Edit: Thanks for the comments, as suggested, I found a better solution:

finaldata = NULL

for (i in 1:n) {

df <- function(xyz)
rbind(finaldata, df)

}

My intention was to bind a list of dataframes together to finaldata in a separate step, but with this solution I don’t need a list in the first place.

>Solution :

Making the comment of @mhovd a bit more specific:

listofdf <- vector("list", length = n)
names(listofdf) <- paste0("df_", seq_len(n))

for (i in seq_len(n)) {
  do something smart that generates df temp
  listofdf[[i]] <- temp
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading