Thomas Philips
Thomas Philips

Reputation: 1089

dplyr group_by and replace all columns with a function of the group

Minimal example - a data frame with responses to 3 questions and a Delay, with the respondent's sex. I'd like to replace the responses to each of the questions with the average response for the respondent's sex.

df <- data.frame(Sex = c(rep("F", 5), rep("M", 5)),
                 Q1  = sample(0:10, 10, replace=T),
                 Q2  = sample(0:10, 10, replace=T),
                 Q3  = sample(0:10, 10, replace=T),
                 Delay = 1:10 
)
QUESTIONS <- c("Q1", "Q2", "Q3")

I then try to replace all the reponses by the average response by sex and wrote

df %>%
  group_by(Sex) %>%
  mutate(all_of(QUESTIONS), mean)
  ungroup()

but get an error message that I can't decipher

Error: Problem with `mutate()` input `..1`.
x Input `..1` can't be recycled to size 5.
i Input `..1` is `all_of(QUESTIONS)`.
i Input `..1` must be size 5 or 1, not 3.
i The error occurred in group 1: Sex = "F".

What is the error in my mutate statement, and how might I resolve it. I have tried variants of all_of such as across(all_of(QUESTIONS)) and replacing mean with colMeans without success. It seems to me that I am making an elementary error with syntax, but am stuck, and would appreciate some guidance.

Sincerely and with many thanks in advance

Thomas Philips

Upvotes: 0

Views: 602

Answers (1)

Thomas Philips
Thomas Philips

Reputation: 1089

My mistake - just misplaced the parentheses. Should have written df %>% group_by(Sex) %>% mutate(across(QUESTIONS, mean)) %>% ungroup(). Apologies.

Upvotes: 1

Related Questions