6

I have a list of data frames, and I want to create a character vector that contains the data frame names that meet a certain criteria. In this case, I want the names of the data frames that contain at least 1 observation. I need these names in a vector for use in another function later on.

The list of data frames is created automatically by the split function. For reproducibility, here's a list of data frames:

df1 <- data.frame(replicate(2,sample(0:1,5,rep=TRUE)))
df2 <- data.frame(replicate(2,sample(0:1,5,rep=TRUE)))
df3 <- data.frame(replicate(0,sample(0:1,5,rep=TRUE))) #empty

mylist <- list(df1, df2,df3) #create list of data frames

mylist2 <- mylist[sapply(mylist, function(x) dim(x)[1]) > 0] #subset list so that df's with no observations are dropped

Now, what I need is a character vector with the names of the data frames in mylist2. (In this example, "df1" and "df2".)

Thanks!

3
  • You have to provide your list of dataframes with the names. Commented Jul 6, 2017 at 15:43
  • Here is one option lapply(Filter(length, mylist), names) Commented Jul 6, 2017 at 15:46
  • Possible duplicate of stackoverflow.com/questions/44944307/… Commented Jul 6, 2017 at 15:48

2 Answers 2

3

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Your second and third line are equivalent to names(mylist)[sapply(mylist, nrow) > 0].
2

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"

2 Comments

Thanks! The issue is that I have over 250 data frames. Is there a simple way to assign each data frame a name without having type out "df1 = df1....df250 = df250"? (I realize this is out of the scope of the original question but a quick search yields no helpful results.)
If the list has already been created then you could do: names(mylist) <- paste0("df", 1L:length(mylist))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.