4

I've been working on several hundred files, which I automatically loaded into the workspace as separate dataframes (let's assume I have 500 dataframes in my workspace).

I would like to create a list consisting of all dataframes/objects in the workspace and to apply a function on all of them. Of course I could type all the objects manually, but it is not very efficient for hundreds or thousands of dataframes. I was wondering whether there is any way I can use the output of ls() function e.g.:

ls()
[1] "a"    "b"    "c"     "d"                      
[5] "e"    "f"    "g"     "h"       
[9] "i"    "j"    "k"     "l"    
[13] "m"    "n"    "o"     "p"          
...

Unfortunately, when I extract from ls() output, I only end up with a character vector of strings and not a list of dataframes.

I would appreciate your ideas. Thanks.

EDITED: the following page How do I make a list of data frames in r gives some background but it doesn't answer my question as it doesn't cover large amounts of dataframes.

Community
  • 1
  • 1
simtim
  • 211
  • 2
  • 12
  • 1
    Instead of creating a lot of dataframe objects, it may be easier to load all the datasets in the working directory to a list. i.e. `files – akrun Feb 04 '15 at 15:12
  • I would suggest using `eval(parse(text= ...` in conjunction with the `ls()` character vector – esa606 Feb 04 '15 at 15:13
  • @akrun Thanks for your idea. Yes, it may be easier for this particular problem, but I would also like to have each dataframe as a separate object. The issue is I want to run a set of functions on all dataframes of interest, and some of them will be also used for further analysis, so I'm just trying to multitask and think of the most efficient workflow. – simtim Feb 04 '15 at 15:16
  • @SImon You could run all the set of functions within the list itself and then weed out the ones you don't want based on some criteria. I would not have 500 dataframe objects. If you need to later save the subset of list as separate files, that is more easier in the list. – akrun Feb 04 '15 at 15:17
  • 1
    @akrun I just tested your 'mget(ls())' approach and it worked brilliantly for thousands of small dataframes. Thank you for this! It needed a bit of tidying up as the code takes all objects into the list from the workspace including functions and values, but it does exactly what I wanted. Thanks again. – simtim Feb 04 '15 at 15:29
  • @Simon You could filter out all the objects that are not data.frames from the global environment. Check this link for an approach http://stackoverflow.com/questions/28142088/how-to-exclude-only-the-data-frames-from-the-global-environment-in-r – akrun Feb 04 '15 at 15:31
  • It is exactly what I proposed but with less lines of codes. Nevermind :) – Colonel Beauvel Feb 04 '15 at 15:34
  • @ColonelBeauvel Yes, your code is compact, but I like the function in the link – akrun Feb 04 '15 at 15:40

3 Answers3

2

Yes you can retrieve the name of all your data.frame using ls, Filter and class. For example suppose you open an R session and type this:

> df1=data.frame(col=1:10)
> df14=data.frame(col=1:10)
> rr=3

You retrieve data.frame names with:

dfnames=Filter(function(x) class(get(x))=='data.frame', ls(env=globalenv()))
#>dfnames
#[1] "df1"  "df14"

And your data.frame list is:

> lapply(dfnames, get)
[[1]]
   col
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9
10  10

[[2]]
   col
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9
10  10

Then you can do what you want with this list.

Colonel Beauvel
  • 28,120
  • 9
  • 39
  • 75
2

, Hi in one shot :

m1 = mtcars
m2 = mtcars
m3 = 1:10
m4 = "blabla"
df_list <- mget(ls()[sapply(ls(), function(x) is.data.frame(get(x)))])

Feel free to rearrange the code in several steps

Victorp
  • 11,816
  • 1
  • 36
  • 48
1

If these data.frames are all that are in your environment you can do:

my_list=sapply(ls(),get)

If you have other objects as well that you don't want to incorporate into your list, you can pick out the data frames of interest using grep().

CephBirk
  • 5,574
  • 4
  • 49
  • 67