1

So I have a bunch of variables in my workspace. I want to assign a subset of them to a new variable, so I can easily run functions on this subset:

workspace:

...
group10
group40
location40
test

desired assignment:

groupList <- list(group10,group40, ...)

intended regular expression:

^group[0-9]+

Any ideas?

1
  • Do you want to assign the names "group10" and "group40" to a new variable, or the values associated with them? Commented Aug 28, 2012 at 23:32

1 Answer 1

2

ls accepts a pattern argument:

group10 <- group40 <- location40 <- test <- NA
mysub <- ls(pattern="^group[0-9]+")
mysub
#[1] "group10" "group40"

You can use lapply to loop over the list of variable names and get their values

groupList <- lapply(mysub, get)

or, in one line

groupList <- lapply(ls(pattern="^group[0-9]+"), get)
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I wasn't aware of get. Thanks, mate!

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.