Taking as a given you have a "large" number of data.frames with similar names (here d# where # is some positive integer), the following is a slight improvement of @mark-miller's method. It is more terse and returns a named list of data.frames, where each name in the list is the name of the corresponding original data.frame.
The key is using mget
together with ls
. If the data frames d1 and d2 provided in the question were the only objects with names d# in the environment, then
my.list <- mget(ls(pattern="^d[0-9]+"))
which would return
my.list
$d1
y1 y2
1 1 4
2 2 5
3 3 6
$d2
y1 y2
1 3 6
2 2 5
3 1 4
This method takes advantage of the pattern argument in ls
, which allows us to use regular expressions to do a finer parsing of the names of objects in the environment. An alternative to the regex "^d[0-9]+$"
is "^d\\d+$"
.
As @gregor points out, it is a better overall to set up your data construction process so that the data.frames are put into named lists at the start.
data
d1 <- data.frame(y1 = c(1,2,3),y2 = c(4,5,6))
d2 <- data.frame(y1 = c(3,2,1),y2 = c(6,5,4))