0

I know how to eval a string, so I can use it as variable.

But I don't know how to 'eval' a string to use it as dataset.

My problem:

I have different datasets with the same structure. The names of the datasets are for example data010 to data019. I want to bind them (via bind_rows).

This is not working:

for (i %in% c(10:19)){
   if (i == 1)
      df <- eval(paste("data0",i)) # eval(parse(paste("data0",i))) is also not working
    else
      df <- df %>% bind_rows(eval(paste("data0",i)))
}

So how to eval a dataset's name given as string?

M--
  • 25,431
  • 8
  • 61
  • 93
  • 1
    Try `bind_rows(mget(paste0("data0", 10:19)))` – akrun Jan 07 '20 at 21:32
  • 3
    Yikes. How did you wind up with a bunch of variables named `data010`, `data011`, `data012` etc? Sounds like those values probably should have been read into a list in R. Then it would be much easier to work with them. When code is written in an R-like way, you very rarely ever need `eval`, `get`, or `assign`. I think the real problem here is how you've read in the data. – MrFlick Jan 07 '20 at 21:35
  • @MrFlick: yes youre right. Maybe it is a little bit more complicated: The datasets I want to combine are the data of different years. I want to read them in to a shiny app to viziualise trend over time. I don't think it is possible to store the several RData-files into a list. But maybe I'm just not familiar with that :-) – Volker Holzendorf Jan 08 '20 at 06:37
  • 1
    @akrun: Thats it. Thankyou. – Volker Holzendorf Jan 09 '20 at 07:38

0 Answers0