无法在dplyr管道内计算标准偏差

时间:2018-11-07 17:55:24

标签: r dplyr

我正在尝试计算一些基本统计数据。计算平均功,但不计算标准差。

  df%>% 
  group_by(date, name) %>% 
  dplyr::summarise(rating = mean(rating, na.rm = T),
                   sd_rating = sd(rating, na.rm = T))
  #And this is working if I apply it on single vector?! 
  sd(df[df$date== "2016-07-22",]$rating)  

示例数据

df <- structure(list(bloc = c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 
5L, 6L), name = c("nn", "nn", "nn", "nn", "nn", "nn", "nn", "nn", 
"nn", "nn", "nn", "nn"), date = c("2016-07-22", "2016-07-22", 
"2016-07-22", "2016-07-22", "2016-07-22", "2016-07-22", "2016-07-29", 
"2016-07-29", "2016-07-29", "2016-07-29", "2016-07-29", "2016-07-29"
), rating = c(0, 0, 0, 0, 0, 0, 0, 0.1, 0, 0, 0, 0)), row.names = c(3L, 
86L, 133L, 206L, 280L, 305L, 339L, 422L, 469L, 542L, 616L, 641L
), class = "data.frame")

0 个答案:

没有答案