在大量数据框中按年份过滤
Filtering by year in a Large List of dataframes
我有大量数据框列表,我想按特定年份过滤数据框中的列表。
DateAndTime 是一个包含 Y-M-D H:M:S 的列,我想仅按年份过滤数据帧列表。
plot_list %>% lapply(dplyr::filter(plot_list, format(DateAndTime, "%Y") == year))
我曾尝试使用列表中的 lapply,但它似乎不起作用。任何帮助,将不胜感激。谢谢。
如果我们在 plot_list
上循环,我们不需要在 filter
上再次使用 plot_list
。在 base R
中,可以使用 subset
来完成
plot_list_sub <- lapply(plot_list, subset,
subset = format(DateAndTime, "%Y") == year)
或使用lambda/anonymous
函数
plot_list_sub <- lapply(plot_list, function(x)
subset(x, format(DateAndTime, "%Y") == year))
或者如果我们想要tidyverse
,用map
(来自purrr
)循环list
并在[=20=上使用filter
] 元素。在这里,我们使用 lambda 函数 (~
),其中 .x
是 list
中的元素
library(dplyr)
library(purrr)
plot_list_sub <- map(plot_list, ~ .x %>%
filter(format(DateAndTime, "%Y") == year))
我有大量数据框列表,我想按特定年份过滤数据框中的列表。
DateAndTime 是一个包含 Y-M-D H:M:S 的列,我想仅按年份过滤数据帧列表。
plot_list %>% lapply(dplyr::filter(plot_list, format(DateAndTime, "%Y") == year))
我曾尝试使用列表中的 lapply,但它似乎不起作用。任何帮助,将不胜感激。谢谢。
如果我们在 plot_list
上循环,我们不需要在 filter
上再次使用 plot_list
。在 base R
中,可以使用 subset
plot_list_sub <- lapply(plot_list, subset,
subset = format(DateAndTime, "%Y") == year)
或使用lambda/anonymous
函数
plot_list_sub <- lapply(plot_list, function(x)
subset(x, format(DateAndTime, "%Y") == year))
或者如果我们想要tidyverse
,用map
(来自purrr
)循环list
并在[=20=上使用filter
] 元素。在这里,我们使用 lambda 函数 (~
),其中 .x
是 list
library(dplyr)
library(purrr)
plot_list_sub <- map(plot_list, ~ .x %>%
filter(format(DateAndTime, "%Y") == year))