如何在清单上使用mutate?

Ril*_*n42 4 r dplyr

更改列表的正确方法是什么?在这种情况下,列表由split

library(dplyr)
csv<-data.frame(participant_number=c(1,1,1,2,2),SelfEsteem=c(3,4,2,1,3))
csv<-csv%>%split(.,.$participant_number)%>%mutate(.,var(.$SelfEsteem))
Run Code Online (Sandbox Code Playgroud)

错误:

Error in UseMethod("mutate_") : 
  no applicable method for 'mutate_' applied to an object of class "list"
Run Code Online (Sandbox Code Playgroud)

更新为了回答评论:我打算计算SelfEsteem每个组的方差(分组var为participant_number)。如果尝试group_by,将无法获得预期的结果。

library(dplyr)

#Please note I changed the dataframe to make my point about variance differences more obvious
csv<-data.frame(participant_number=c(1,1,1,2,2),SelfEsteem=c(3,4,2,1,3))

csv<-csv%>%group_by(participant_number)%>%mutate(.,SE_variance=var(.$SelfEsteem))

#var(c(3,4,2)) #1
#var(c(1,3))  #2
Run Code Online (Sandbox Code Playgroud)

预期:

        participant_number SelfEsteem SE_variance
(dbl)            (dbl)       (dbl)
1                  1          3         1
2                  1          4         1
3                  1          2         1
4                  2          1         2
5                  2          3         2
Run Code Online (Sandbox Code Playgroud)

Gre*_*gor 5

的尝试group_by失败,因为您覆盖mutate的搜索路径。Mutate使用非标准评估,因此它将首先在其data参数的列中查找变量。

当使用管(%>%)中,A点.指的是在整个数据帧,并且.$SelfEsteem指的是整个从自尊柱整个数据帧。

您只需要简化一点(不要覆盖默认值)即可获得预期的结果。

csv %>% 
  group_by(participant_number) %>%
  mutate(SE_variance = var(SelfEsteem))
# Source: local data frame [5 x 3]
# Groups: participant_number [2]
# 
#   participant_number SelfEsteem SE_variance
#                (dbl)      (dbl)       (dbl)
# 1                  1          3           1
# 2                  1          4           1
# 3                  1          2           1
# 4                  2          1           2
# 5                  2          3           2
Run Code Online (Sandbox Code Playgroud)