List objects in the global environment that match a specific string pattern

I have 10 data frames in the global environment - 5 with a naming pattern and another 5 with a different naming pattern. I want to put data files with the same naming pattern in a list each (two lists - one for each pattern), so I can end up running checks for each one with the lapply

following:

 lapply(listofdataframes, function(x) range(x[ , "date"]))`

      

Thus, the names of the templates - Pattern 1

: q32013local

, q42013local

, q12014local

etc.

Pattern 2

: q32013national

, q42013national

etc.

I've used this in the past:

 Filter(function(x) is(x, "data.frame"), mget(ls()))` 

      

but it obviously lists all dataframes in the global environment.

I was looking for how to use grep

and ls

together. I found equivalent questions bash

for this in SO here List of files with specific extensions with ls and grep , but not the R equivalent. I touched on these two related questions, but they are completely different:

Returns the list items as independent objects in the global environment . How can I list all the data that is in my global environment?

+3


source to share


1 answer


I used the following, obviously this will need to be repeated for every template.



Pattern1<-grep("local",names(.GlobalEnv),value=TRUE)
     Pattern1_list<-do.call("list",mget(Pattern1))

      

+2


source







All Articles