0

I have a list of variables

l <- list(1,2,3)

From which I am extracting each variable using

list2env(setNames(l,paste0("var",seq_along(l))), envir = parent.frame()) 

which creates var1, var2, var3.

Now I need to pass to a function

list(var1, var2, var3)

How can I do that dynamically? For example if the list is

l <- list(1,2,3,4)

I don't want to change my code to

list(var1, var2, var3, var4)

Thanks!

3
  • Can you show the final result you want? I'm a bit confused by what you mean by "variables" in the first sentence. Commented Dec 18, 2020 at 20:41
  • I would give serious consideration as to whether you shouldn't just keep these items in a list in the first place. One of the great things about working with objects in lists is that you don't need to take them out and put them back in. Anything you can do with an object that isn't in a list can be done with an object that is, plus you preserve the ability to iterate directly over its members and don't have lots of rubbish polluting your workspace. Commented Dec 18, 2020 at 20:47
  • Thanks, the answer above is what I was looking for. I know this is not ideal, but I need to adapt to existing (and non-modifiable) input and output functions... Commented Dec 18, 2020 at 20:52

1 Answer 1

2

Try:

l <- list(1,2,3,4)
list2env(setNames(l,paste0("var",seq_along(l))), envir = parent.frame()) 
List <- mget(ls(pattern = 'var'))
Sign up to request clarification or add additional context in comments.

Comments

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.