1.
同时计算三个列,又能同时创建三个新列并命名。
原列名:“Wins”,“Losses”,“Draws”
新增列列名:“Wins_rt”,“Losses_rt”,“Draws_rt”
mutate(across(c(Wins,Losses,Draws),~.x/Total,.names = '{.col}_rt'))
2.
原列名改为:“Wins_mn”,“Losses_mn”,“Draws_mn”
summarise(across(where(is.numeric),~mean(.x,na.rm=TRUE),.names = "{sub('rt', 'mn', col)}"))
3.
df %>%
summarise(across(starts_with(c("Wins","Losses","Draws")),
list(md=median,mn=mean),
.names="{substring(.col,1,1)}_{.fn}"))
# 结果是列名为以starts_with里的字符串第一个字母加上list里的名字
# 即:W_md,W_mn,L_md,L_mn,D_md,D_mn