Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make list of objects in global environment matching certain string pattern

Tags:

regex

list

r

I have 10 data frames in the global environment- 5 with a name pattern and other 5 with another naming pattern. I want to put the dataframes with same naming pattern into a list each (two lists - one for each pattern) so ultimately I can run checks on each of them using lapply like this :

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

The naming patterns are thus - Pattern 1 : q32013local, q42013local, q12014local, etc.

Pattern 2 : q32013national, q42013national etc.

I have used this in the past:

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

but it obviously makes a list of all data frames in global environment.

I was looking for how to use grep and ls together . I found the bash equivalent questions for it on SO here List files with certain extensions with ls and grep but no R equivalent. I did refer these two related questions but they are quite different :

Return elements of list as independent objects in global environment , How can I make a list of all dataframes that are in my global environment?

like image 865
vagabond Avatar asked Nov 04 '14 14:11

vagabond


1 Answers

I have used the following, obviously this will need to be repeated for each pattern.

Pattern1<-grep("local",names(.GlobalEnv),value=TRUE)
     Pattern1_list<-do.call("list",mget(Pattern1))
like image 120
W. Kessler Avatar answered Oct 04 '22 22:10

W. Kessler