25

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?

oguz ismail
  • 1
  • 16
  • 47
  • 69
vagabond
  • 3,526
  • 5
  • 43
  • 76
  • @vagabond You can extract the `local` and `national` and split on those – akrun Nov 04 '14 at 15:01
  • But you showed two patterns. So what I thought was you need all datasets with a particular pattern in one list and another in different list or a sort of nested list. – akrun Nov 04 '14 at 15:26
  • Could you post the error as well. – akrun Nov 04 '14 at 15:28
  • `Error in mget(grep(pattern = "q.*local", as.character(ls()))) : invalid first argument` . Sorry I didn't mention that clearly in the question - I wanted to make two lists - one for each pattern. – vagabond Nov 04 '14 at 15:39
  • 3
    After creating some datasets, I was able to do this comfortably using `mget(ls(pattern="q\\d+local"))`, but why do you need `grep` (not tested yet) – akrun Nov 04 '14 at 15:40
  • yes, u r right . . I don't need the `grep` at all ! please post as an answer. – vagabond Nov 04 '14 at 15:43
  • 2
    With object names like that I would suggest you *begin* the session by putting them into a list – Rich Scriven Nov 04 '14 at 15:46
  • @vagabond As mentioned by Richard Scriven, I would also have read it like `lst <- lapply(list.files(pattern='^q\\d+'), function(x) read.table(x, header=TRUE))` Please change the pattern accordingly. – akrun Nov 04 '14 at 15:48
  • yes, but i wanted to individually clean each data frame before putting them all into a list. Each of them has specific problems and I am not very hands on with adding, removing columns, merging with other data frames while keeping everything in a list. I know it is the right way to go but I'm not there yet ! – vagabond Nov 04 '14 at 16:04

2 Answers2

18

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))
W. Kessler
  • 355
  • 3
  • 9
1

This is a shorter solution inspired in the one from W.Kessler:

Pattern1_list <- list(mget(ls(pattern = "local")))[[1]]
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129