I just got this example df:
df = data.frame(A1 = c(1,2,3),
A2 = c(4,5,6))
And I'm trying to divide each of these "A" columns by the average of the entire column
First of all, i need a mean
column:
df %>%
mutate(
across(matches("A"), ~ mean(.x), .names = "mean_{col}" )
)
But, based on this question: Mutate across multiple columns to create new variable sets, i can't divide the A's columns by the mean. I'm trying to do:
df %>%
mutate(
across(matches("A"), ~ mean(.x), .names = "mean_{col}" )
) %>%
mutate(
across(matches("A"), .names = "adm_A_{col}") / across(matches("mean_"))
)
What i'm doing wrong?