dplyr group_by并用组的函数替换所有列

dplyr group_by并用组的函数替换所有列,r,dplyr,R,Dplyr,最简单的例子-一个数据框,包含对3个问题的回答和延迟,以及受访者的性别。我想用受访者性别的平均回答来代替对每个问题的回答 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),

最简单的例子-一个数据框,包含对3个问题的回答和延迟,以及受访者的性别。我想用受访者性别的平均回答来代替对每个问题的回答

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")
但是得到一条我无法破译的错误信息

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".
我的mutate语句中有什么错误,以及如何解决它。我尝试了
all\u of
的变体,例如
跨越(all\u of(QUESTIONS))
并将
mean
替换为
colMeans
,但没有成功。在我看来,我在语法上犯了一个基本的错误,但我被卡住了,希望能得到一些指导

谨此致以诚挚的谢意


托马斯·菲利普斯(Thomas Philips)

我的错误——只是把括号放错地方了。应该写
df%>%group\u by(性别)%%>%mutate(跨越(问题,平均值))%%>%ungroup()
。抱歉

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".