首页 > 解决方案 > 如何将均值、中值和标准差合并到数据表的输出中?

问题描述

我有一些 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

标签: r

解决方案


推荐阅读