My question builds on a similar one by imposing an additional constraint that the name of each variable should appear only once.
Consider a data frame
Use .[[i]]
and !!names(.)[i]:=
to refer to the ith column and its name.
library(tibble)
library(dplyr)
library(rlang)
df %>% summarize(!!names(.)[1] := mean(.[[1]]), !!names(.)[2] := sum(.[[2]]))
giving:
# A tibble: 1 x 2
potentially_long_name_i_dont_want_to_type_twice another_annoyingly_long_name
1 5.5 255
If df were grouped (it is not in the question so this is not needed) then surround summarize
with a do
like this:
library(dplyr)
library(rlang)
library(tibble)
df2 <- tibble(a = 1:10, b = 11:20, g = rep(1:2, each = 5))
df2 %>%
group_by(g) %>%
do(summarize(., !!names(.)[1] := mean(.[[1]]), !!names(.)[2] := sum(.[[2]]))) %>%
ungroup
giving:
# A tibble: 2 x 3
g a b
1 1 3 65
2 2 8 90