Really appreciate the for loop solution as often running into this situation myself and I always wondered if you can do this without running the same code multiple times. This has been super helpful!
You can accomplish it with a single pivot longer if the column names have a consistent pattern/separator. I gave the stat variables a new suffix ("_score") to make that happen. fake_dat |> rename_with(\(x) str_replace(x, "(.+\\d$)", "\\1_score")) |> pivot_longer( c(ends_with("_score"), ends_with("_n")), names_to = c("stat", ".value"), names_sep = "_", ) |> summarize( total_obs = sum(n), wgt_stat = weighted.mean(score, n), .by = c(athlete, stat) )
Also, thank you both for this video!
Really appreciate the for loop solution as often running into this situation myself and I always wondered if you can do this without running the same code multiple times. This has been super helpful!
Keeping Patrick on his toes :)
You can accomplish it with a single pivot longer if the column names have a consistent pattern/separator. I gave the stat variables a new suffix ("_score") to make that happen.
fake_dat |>
rename_with(\(x) str_replace(x, "(.+\\d$)", "\\1_score")) |>
pivot_longer(
c(ends_with("_score"), ends_with("_n")),
names_to = c("stat", ".value"),
names_sep = "_",
) |>
summarize(
total_obs = sum(n),
wgt_stat = weighted.mean(score, n),
.by = c(athlete, stat)
)
If you must use a for loop...
# Using a for loop
library(rlang)
wgt_stat_list