0
我想按组最大追加到观测台新的变量,e.g:变异基于组级别统计
iris %>% split(iris$Species) %>%
lapply(function(l) mutate(l, species_max = max(Sepal.Width))) %>%
bind_rows() %>% .[c(1,51,101),]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species species_max
1 5.1 3.5 1.4 0.2 setosa 4.4
51 7.0 3.2 4.7 1.4 versicolor 3.4
101 6.3 3.3 6.0 2.5 virginica 3.8
是否有一个更优雅的解决方案dplyr::group_by
来实现这一目标?
你可以purrr-IFY它:'库(tidyverse);虹膜%>%split(。$物种)%>%map(mutate,species_max = max(Sepal.Width))%>%map_df(slice,1)''或者更好,走相反的路线并嵌套:'iris% >%mutate(species_max = map_dbl(data,〜max(.x $ Sepal.Width)),data = map(data,slice,1))%>%unnest()'>。或者把它全部放在严格的dplyr中,然后用docendo的答案去解决。 – alistaire
@Axeman阅读标题 - 平均值/最大值几乎不是问题 – geotheory