Christoph Safferling
Christoph Safferling

Reputation: 1186

how to get list of data frames out of the environment for later lapply() magic?

I would like to get an automated way to get the nrow() of a subset of my many datasets. I have a large number of DFs that all follow a naming scheme, and I would like to run nrow() over each of these. This is what I have come up with:

# generate two relevant and one irrelevant DF
test_5 <- data.frame(rnorm(5))
test_10 <- data.frame(rnorm(10))
irrelevant_df <- data.frame(rnorm(5))

# I want this, but automatically over all test_ DFs
nrow(test_5)
nrow(test_10)

# get all DFs that follow the 'test_' naming scheme
v <- grep('test_', ls(), value = TRUE)
v
l <- as.list(grep('test_', ls(), value = TRUE))
l

# works, but is manual
sapply(list(test_5, test_10), function(x) nrow(x))
# doesn't work (returns NULL), because l,v is stored as chr?
sapply(v, function(x) nrow(x))
sapply(l, function(x) nrow(x))

Is there a way to get the objects from ls() so that I can push the results to {s,l}apply functions?

As a bonus question, is it possible to get the list of ls() objects in order? Because v stores it in alphabetical order, so a later rbind(v, sapply(...)) gives the wrong results.

Any pointers are greatly appreciated!

Upvotes: 2

Views: 485

Answers (1)

David Arenburg
David Arenburg

Reputation: 92292

Try

lapply(mget(ls(pattern = "^test")), nrow)
## $test_10
## [1] 10
##
## $test_5
## [1] 5

You can use data.frame or do.call + rbind combination too if you want then combined, such as

data.frame(lapply(mget(ls(pattern = "^test")), nrow))
##   test_10 test_5
## 1      10      5 

Or

do.call(rbind, lapply(mget(ls(pattern = "^test")), nrow))
##         [,1]
## test_10   10
## test_5     5

Upvotes: 4

Related Questions