Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个列表state-list,其中包含 4 个列表wa、和tex,所有这些列表都包含大约 60 个数据帧。我想对这些数据框应用相同的功能。例如,我想添加一个具有平均值的新列,如下所示:cinohi
state-list
wa
tex
cin
ohi
library(dplyr) df # example df from one of the lists df %>% group_by(x) %>% mutate(mean_value = mean(value))
我怎样才能做到这一点?
我们可以使用嵌套map来循环list
map
list
library(purrr) library(dplyr) out <- map(state_list, ~ map(.x, ~ .x %>% group_by(x) %>% mutate(mean_value = mean(value)))
或使用base R
base R
out <- lapply(state_list, function(lst1) lapply(lst1, function(dat) transform(dat, mean_value = ave(value, x))))