首页 > 解决方案 > Mean/sd: Figure out the mean and standard deviation for each group. And then the one closer to one of the groups is assigned that

问题描述

This is my data.

I'm trying to figure out the mean/sd of groups A and groups B, but I'm at a loss:

library(ggplot2)
n <- 10000
df <- data.frame(
  cats=rep(c("A","B"), each=n), 
  vals=c(rnorm(n, mean=10, sd=2), rnorm(n, mean=20, sd=2))
)
ggplot(df, aes(vals, color=cats)) +
  geom_density()

标签: rstatisticsprobability

解决方案


您可以使用 dplyr::summarize

library(dplyr)
df %>% group_by(cats) %>% summarize(sd = sd(vals), mean = mean(vals))

cats     sd  mean
<fct> <dbl> <dbl>
1 A      1.99  10.0
2 B      1.98  20.0

推荐阅读