0

I have some R code that allows me to view percentiles. The code is:

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')

I tried the following:

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')

Unfortunately, this creates a separate output for mean. I'm really looking to get a mean column binded to the right-side of the percentile output.

How would I go about adding the mean, median and standard deviation values to the percentile output?

Thanks in advance!

EDIT: a sample of the data is as follows:

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
equanimity
  • 2,371
  • 3
  • 29
  • 53

0 Answers0