0

I would like to read a data frame from a string. It is a bit difficult to explain, and, so far I couldn't find the answer anywhere.

Let's imagine I have several data frames: "Param1", "Param2", ... "Param100". I would like to be able to access them from a for loop:

for (i in c(1:100)){
  a <- paste0("Param",i)
  b <- ??? (a)
}

Where b would become "Param1", ... "Param100" when i varies from 1 to 100.

2

1 Answer 1

2

Use get.

for (i in 1:100) {
  b <- get(paste0("Param", i))
}
Sign up to request clarification or add additional context in comments.

6 Comments

Cant you just use mget without a loop?
Yes, you could. mget(paste0("Param", 1:100)) I believe would work. You'll end up with a list to operate on.
More like mget(ls(pattern = paste0("Param", 1:100)))
Oops, thanks David. I've fixed my comment of a bug. Its a bit easier than your suggestion though, the ls part isn't necessary I think.
I think it is necessary.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.