如何在我使用 ddply 的以下上下文中使用 dplyr 函数

how to use dplyr function in the following context where I use ddply

以下是我正在尝试做的事情:

dput(dat)
structure(list(group = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("group1", "group2", 
"group3"), class = "factor"), value = c(34L, 143L, 36L, 23L, 
134L, 24L, 28L, 120L, 36L, 24L, 155L, 43L, 25L, 145L, 12L)), .Names = c("group", 
"value"), row.names = c(NA, -15L), class = "data.frame")

> dat %>% ddply(.(group), function(x){sum((x$value-mean(x$value))^2)}) %>% .[["V1"]] %>% sum()
[1] 1372.8

基本上,按组计算平方和并将结果相加。 当我尝试使用 dplyr 实现相同的目标时,出现以下错误:

> dat %>% group_by(group) %>% do(function(x) {x$value-mean(x$value)})
Error: Results are not data frames at positions: 1, 2, 3

您可以尝试使用 summarise,提取 "V1" 列和 sum

dat %>% 
    group_by(group) %>% 
    dplyr::summarise(V1=sum((value-mean(value))^2))%>%
    .$V1 %>% 
    sum()
#[1] 1372.8

也许试试

library(dplyr)
dat %>% 
  group_by(group) %>% 
  summarise(V1 =  sum((value - mean(value))^2)) %>% 
  summarise(V1 = sum(V1)) %>% 
  .$V1
# [1] 1372.8

或者,如果你想要 do

dat %>% 
  group_by(group) %>% 
  do({data.frame(V1 = sum((.$value-mean(.$value))^2))}) %>% 
  ungroup() %>% 
  summarise(V1 = sum(V1)) %>% 
  .$V1
# [1] 1372.8