如何将均值,中位数和标准差合并到数据表的输出中?

时间:2019-04-17 14:27:43

标签: 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

0 个答案:

没有答案