0

I thought this would be easy but I can't figure it out.

I have 100+ data frames in my global environment. All names are just random UUIDs (generated by a different program) and I want to loop over all of them to add a row or something.

I use ls() to get the names of all of them. And then I don't know how I'm supposed to get data from them. I can easily print all of the names:

frames <- ls(pattern = "Results")
for (i in frames) {
  print(i)
}

But I don't see how I can get any columns because all I have is the string of the name of the data frame, not a pointer to the data frame itself.

Any help would be appreciated.

CodedGames
  • 11
  • 2

1 Answers1

0

Illustrating with data frames from the datasets package, we can use lapply() and an anonymous function that leverages get() to access a list of data frames whose names are stored in a vector.

library(datasets)
dsList <- c("airmiles","airquality","attitude")

lapply(dsList,function(x) head(get(x)))

...and the output:

> lapply(dsList,function(x) head(get(x)))
[[1]]
[1]  412  480  683 1052 1385 1418

[[2]]
  Ozone Solar.R Wind Temp Month Day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4
5    NA      NA 14.3   56     5   5
6    28      NA 14.9   66     5   6

[[3]]
  rating complaints privileges learning raises critical advance
1     43         51         30       39     61       92      45
2     63         64         51       54     63       73      47
3     71         70         68       69     76       86      48
4     61         63         45       47     54       84      35
5     81         78         56       66     71       83      47
6     43         55         49       44     54       49      34

Note: posting as community wiki since this expands on a comment to the OP.

Len Greski
  • 10,505
  • 2
  • 22
  • 33