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
.
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.
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.
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.
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"))
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].)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With