修订版 | fb6dbc5e9111c1e79844e23177bc496b760d29e2 (tree) |
---|---|
时间 | 2020-09-19 05:49:07 |
作者 | Lorenzo Isella <lorenzo.isella@gmai...> |
Commiter | Lorenzo Isella |
I had saved the wrong solution!
@@ -14,10 +14,9 @@ | ||
14 | 14 | |
15 | 15 | ## See https://stackoverflow.com/questions/63959015/dplyr-and-summarising-according-to-presence-or-not-of-an-element |
16 | 16 | |
17 | - | |
18 | 17 | df_summary <- df %>% |
19 | - distinct(year) %>% | |
20 | - anti_join(has_d, by = "year") %>% | |
21 | - mutate(ranking = -1L) %>% | |
22 | - bind_rows(has_d) %>% | |
23 | - arrange(year) | |
18 | + mutate(is_d = (x=="d")*1) %>% | |
19 | + group_by(year) %>% | |
20 | + summarise(Val=if_else(sum(is_d)==1, | |
21 | + sum(is_d*ranking), | |
22 | + -1)) |