Extract dataframes from lists with dataframes under given criteria in R

Asked

Viewed 69 times

5

I organized my list of dataframes like this:

df.1<-as.data.frame(matrix(rnorm(10),10,80))
df.1.in.list<-split.default(df.1, gl(10, 8))

On the list df.1.in.list i would like to keep the dataframes where the first element of column 8 is 2 or -2. Keeping the list character for df.1.in.list

That is, remove dataframes that do not meet this criterion.

There are probably no such cases in this example I generated. But my original dataframe has cases like this. I’d really like the idea to promote this selection.

How do I do that?

1 answer

5


You can use the function keep of purrr:

library(purrr)

df.1.in.list %>%
  keep(~.x[1,8] %in% c(-2, 2))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.