How can I make a list of all dataframes that are in my global environment?

The ls function lists all things in your environment. The get function gets a variable with a given name. You can use the class function to get the class of a variable.

If you put them all together, you can do this:

ls()[sapply(ls(), function(x) class(get(x))) == 'data.frame']

which will return a character vector of the data.frames in the current environment.


From your posted code, I would recommend you start a new R session, and read the files in again with the following code

do.call(rbind, lapply(list.files(pattern = ".csv"), read.csv))

This function should return a proper list with all the data.frames as elements

dfs <- Filter(function(x) is(x, "data.frame"), mget(ls()))

then you can rbind them with

do.call(rbind, dfs)

Of course it's awfully silly to have a bunch of data.frames lying around that are so related that you want to rbind them. It sounds like they probably should have been in a list in the first place.

I recommend you say away from assign(), that's always a sign things are probably afoul. Try

temp <- list.files(pattern="*.csv")
dfs <- lapply(temp, read.csv)

that should return a list straight away.

Tags:

R