I am using the following code, which calculates z scores per subgroup. I would like to add one more column at the end, which sums the z scores, so row-wise sums of all the values in all the columns that end with "_zscore". How could I specifically select those columns?
(Note that my real data have many more columns, so I am looking to specifically select "_zscore" in column names.)
library(dplyr)
set.seed(12345)
df1 = data.frame(a=c(rep("a",8), rep("b",5), rep("c",7), rep("d",10)),
b=rnorm(30, 6, 2),
c=rnorm(30, 12, 3.5),
d=rnorm(30, 8, 3)
)
df1_z <- df1 %>%
group_by(a) %>%
mutate(across(b:d, list(zscore = ~as.numeric(scale(.)))))