r - 如何将均值、中值和标准差合并到数据表的输出中?
问题描述
我有一些 R 代码可以让我查看百分位数。代码是:
library(dplyr)
library(data.table)
# working directory
setwd("C:/Users/jdoe/Desktop/Scripts")
# reads the file
df <- fread("customer-stats.csv",
header = TRUE,
fill = TRUE,
sep = ",")
qs = df[AvgValue > 0, .(Samples = sum(Samples),
'50th' = quantile(AvgValue, probs = c(0.50)),
'99th' = quantile(AvgValue, probs = c(0.99)),
'99.9th' = quantile(AvgValue, probs = c(0.999)),
'99.99th' = quantile(AvgValue, probs = c(0.9999))),
by = .(Name, Address)]
setkey(qs, 'Name')
我尝试了以下方法:
qs = df[AvgValue > 0, .(Samples = sum(Samples),
'50th' = quantile(AvgValue, probs = c(0.50)),
'99th' = quantile(AvgValue, probs = c(0.99)),
'99.9th' = quantile(AvgValue, probs = c(0.999)),
'99.99th' = quantile(AvgValue, probs = c(0.9999)),
'Mean' = mean(AvgValue)),
by = .(Name, Address)]
setkey(qs, 'Name')
不幸的是,这会为均值创建一个单独的输出。我真的很想得到一个绑定到百分位输出右侧的平均列。
我将如何将平均值、中位数和标准差值添加到百分位数输出中?
提前致谢!
编辑:数据样本如下:
Name Address AvgValue Samples
Exchange /main/UnitedStates 0 0
Exchange /main/UnitedStates 0 0
Exchange /main/England 0 0
Exchange /main/Japan 0 0
Exchange /main/England 9.567738524 23763
Exchange /main/Italy 9.479710598 60485
Exchange /main/France 0 0
Exchange /main/France 9.498684793 349349
Exchange /main/Italy 9.528628692 6968
Exchange /main/UnitedStates 0 0
Exchange /main/Spain 9.483226458 458945
Exchange /main/Sweden 9.502689957 908249
Exchange /main/Germany 9.673584266 31
Exchange /main/France 37.92883138 760
解决方案
推荐阅读
- python - 在第一行插入字符串使用python3
- python - 寻找简单的 Python 公式来组合两个文本文件
- python - 添加任务编号
- python - 如何抓取 YouTube 频道创建者并链接到他们的频道?
- recursion - 在 Rust 中的迭代期间将迭代器传递给递归调用
- git - Git 正在转向新的哈希算法 SHA-256 但为什么 git 社区选择了 SHA-256
- python-3.x - 同时运行多个 Web 浏览器 Selenium Python 3
- python - 如何在 wit.ai 中使用多个实体
- python - 我不应该能够填充通过变换派生的列吗?
- python - 遍历项目列表并保存