diff --git a/iteration.qmd b/iteration.qmd index 62090c688..bbff172b5 100644 --- a/iteration.qmd +++ b/iteration.qmd @@ -393,7 +393,27 @@ df_long |> summarize(mean = weighted.mean(val, wts)) ``` -If needed, you could `pivot_wider()` this back to the original form. +If needed, you could `pivot_wider()` this back to the original form. + +This problem was actually solved on StackOverflow much earlier to release of 2nd edition. + +```{r} +df_paired |> + summarise( + across( + .cols = ends_with("val"), + .fns = ~ weighted.mean(., get(str_replace( + cur_column(), "val", "wts" + ))), + .names = '{str_remove(col, "_val")}' + ) + ) + +#> # A tibble: 1 × 4 +#> a b c d +#> +#> 1 0.180 0.199 -0.208 -0.746 +``` ### Exercises