我有一个包含多个数据框的 list()
,我想对它们全部应用 dplyr
的 filter()
。
以下是我目前尝试过的示例代码...
require(dplyr)
list.DFs <- list(df1,df2)
lapply(
X = list.DFS,
FUN = filter(Gold.fish.count=="Total")
)
但这会出现错误:找不到对象 'Gold.fish.count'
。
使用purrr
library(purrr)
map(list.DFs, ~filter(.x, Gold.fish.count == "Total"))
显然,您可以使用lapply完全相同的方法:
lapply(list.DFs, function(x) filter(x, Gold.fish.count == "Total"))
variable <-
。 - yeedle没有样例数据很难准确地确定您需要什么(在以后查看如何使用dput函数)。但以下的某些变化可能有所帮助:
indices <- sapply(list.DFs, function(x) x$Gold.fish.count == "Total")
list.DFs[indices]
indices <- sapply(list.DFs, function(x) x$Gold.fish.count[1] == "Total")
list.DFs[indices]
lapply(list.DFS, dplyr::filter_, "Gold.fish.count=='Total'")
或者直接在基础R中完成整个过程lapply(list.DFS, subset, Gold.fish.count=='Total')
。 - David Arenburg