Create vector of data frame names from a list of data frames

If you want this to be somewhat automatized, for instance you have a large number of data.frames in your environment, you might want to select them with a pattern matching in one line:

mylist = do.call("list", mget(grep("df", ls(), value=T)))
mylist2 <- mylist[sapply(mylist, function(x) dim(x)[1]) > 0]

This creates the first list with all data.frames that have "df" in their names, while keeping their names as attributes of the list elements.

Then I apply your filter for 1-element data.frames and you just retrieve the names:

names(mylist2)
#### [1] "df1" "df2"

You need to have a named list. Here is how you could do it:

library(dplyr)
library(purrr)

mylist <- list(df1 = df1, df2 = df2, df3 = df3)

test <- mylist %>%
  map_int(nrow)

test[test > 0] %>% names()
#[1] "df1" "df2"

Tags:

List

R

Dataframe