r - 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()
解决方案
您可以使用 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
推荐阅读
- node.js - 在 Docker 映像中将端口 80 公开为用户节点
- c# - 在 DataGridView 中显示不存在的(在数据库表中)列
- html - 在 col 12 div 下的 Bootstrap 表格网格上添加滚动条
- android - 互联网恢复时应用程序崩溃,RxJava & Retrofit
- javascript - 通过kendo mvc实现折线图,水平滚动需要帮助,y轴应该可见
- python - Celery + RabbitMQ 变慢
- cassandra-3.0 - 如何从 Cassandra DevCenter 将结果导出为带有 Header 的 CSV 文件?
- angular - 使用 angular2 材质日期选择器的错误对象对象
- php - 过滤多阵列
- java - Spring Boot + Hibernate + Gradle + Oracle + IntelliJ