How to apply dplyr () filter to list () of data frames?

I have list()data. I want to apply dplyr filter()to all of them.

Sample 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 it gives an error: Object 'Gold.fish.count' not found.

+4
source share
1 answer

Using purrr

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

Obviously, you can do the same with lapply:

lapply(list.DFs, function(x) filter(x, Gold.fish.count == "Total"))
+6
source

Source: https://habr.com/ru/post/1672655/


All Articles