Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply dplyr filter to list of data frames?

I have a list() of dataframes. I want to apply dplyr's filter() to all of them.

Example code of what I have tried so far...

require(dplyr)
list.DFs <- list(df1,df2)
lapply(
  X = list.DFS,
  FUN = filter(Gold.fish.count=="Total")
)

But this gives an error: Object 'Gold.fish.count' not found.

like image 275
Username Avatar asked Mar 19 '17 06:03

Username


People also ask

How do I filter multiple values in R dplyr?

In this, first, pass your dataframe object to the filter function, then in the condition parameter write the column name in which you want to filter multiple values then put the %in% operator, and then pass a vector containing all the string values which you want in the result.

Can you use filter on a data frame in R?

How to apply a filter on dataframe in R ? A filter () function is used to filter out specified elements from a dataframe that return TRUE value for the given condition (s). filter () helps to reduce a huge dataset into small chunks of datasets.

Can you have a list of data frames?

A Data frame is simply a List of a specified class called “data. frame”, but the components of the list must be vectors (numeric, character, logical), factors, matrices (numeric), lists, or even other data frames.


2 Answers

Using purrr

library(purrr)
map(list.DFs, ~filter(.x, Gold.fish.count == "Total"))

Obviously, you can do exactly the same with lapply:

lapply(list.DFs, function(x) filter(x, Gold.fish.count == "Total"))
like image 67
yeedle Avatar answered Oct 12 '22 10:10

yeedle


Without having example data it's hard to identify exactly what you're after (check out how to use dput function in future). But some variation of the following might help:

indices <- sapply(list.DFs, function(x) x$Gold.fish.count == "Total")
list.DFs[indices]

The first line creates a list of logicals (True/False) where your condition is met. These logicals are then used to subset your original list of data frames.

If Gold.fish.count is a column that contains "Total" for every row then you can use an indexing variation:

indices <- sapply(list.DFs, function(x) x$Gold.fish.count[1] == "Total")
list.DFs[indices]

(Notice the additional [1].)

like image 1
Bradford Avatar answered Oct 12 '22 12:10

Bradford