Skip to content

Commit

Permalink
when you have groups of columns
Browse files Browse the repository at this point in the history
when you have groups of columns that you want to compute with simultaneously.
  • Loading branch information
anilyayavar authored Jul 28, 2024
1 parent 643ab1b commit 0c98041
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion iteration.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
#> <dbl> <dbl> <dbl> <dbl>
#> 1 0.180 0.199 -0.208 -0.746
```

### Exercises

Expand Down

0 comments on commit 0c98041

Please sign in to comment.