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?

dmvianna
  • 15,088
  • 18
  • 77
  • 106
  • Do you want to assign the _names_ "group10" and "group40" to a new variable, or the _values_ associated with them? – GSee Aug 28 '12 at 23:32

1 Answers1

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)
GSee
  • 48,880
  • 13
  • 125
  • 145