我想将变量的标准差与一个因子分组后的变量的标准偏差进行比较。
这是整体sd()
require(dplyr)
iris %.% summarise(
Overall.SD = sd(Sepal.Length)
)
但是,一旦我使用group_by
,我就无法访问它iris %.%
group_by(Species) %.%
summarise(
Species.SD = sd(Sepal.Length),
Overall.SD = sd(iris$Sepal.Length),
Species.SD < Overall.SD
)
有没有办法让dplyr回顾整个数据集?
答案 0 :(得分:2)
我会在使用Overall.SD
对数据进行分组之前计算mutate
,以便其他数据保持不变。
iris %>%
mutate(Overall.SD = sd(Sepal.Length)) %>% # you can use mutate instead of summarise here
group_by(Species) %>%
summarise(Species.SD = sd(Sepal.Length),
Overall.SD = Overall.SD[1], # You could also remove this line if you just want the comparison and don't need to display the actual Overall.SD
Species.SD < Overall.SD)