This function provides descriptive statistics for a quantitative variable (unquoted). If no keyword arguments for statistics are provided, default values are used. Default stats: c(“n”, “mean”, “sd”)

dstats(oil,operator)
#>    n     mean      sd
#> 1 53 11522.05 7250.68

You supply statistics argument with the “stats” parameter. You can pass single statistic or multiple statistics as a list object. Singe statistic:

dstats(oil,operator, stats = c("median"))
#>   median
#> 1  16000

Multiple statistics:

dstats(soils, pH, stats = c("median", "mean", "sd"))
#>   median mean   sd
#> 1   4.54 4.67 0.67

You can supply as many grouping variables as needed.

dstats(soils, pH, Group, Contour, Depth)
#>    Group    Contour Depth n mean   sd
#> 1      1        Top  0-10 4 5.33 0.24
#> 2      2        Top 10-30 4 4.85 0.33
#> 3      3        Top 30-60 4 4.20 0.23
#> 4      4        Top 60-90 4 3.89 0.14
#> 5      5      Slope  0-10 4 5.51 0.31
#> 6      6      Slope 10-30 4 5.28 0.95
#> 7      7      Slope 30-60 4 4.27 0.49
#> 8      8      Slope 60-90 4 3.93 0.09
#> 9      9 Depression  0-10 4 5.35 0.22
#> 10    10 Depression 10-30 4 4.88 0.37
#> 11    11 Depression 30-60 4 4.36 0.31
#> 12    12 Depression 60-90 4 4.17 0.25

You can pass multiple statistics and multiple grouping variable at the same time.

dstats(soils, pH, Group, Contour, stats=c("mean", "median", "sd"))
#>    Group    Contour mean median   sd
#> 1      1        Top 5.33   5.27 0.24
#> 2      2        Top 4.85   4.90 0.33
#> 3      3        Top 4.20   4.27 0.23
#> 4      4        Top 3.89   3.88 0.14
#> 5      5      Slope 5.51   5.54 0.31
#> 6      6      Slope 5.28   4.94 0.95
#> 7      7      Slope 4.27   4.06 0.49
#> 8      8      Slope 3.93   3.95 0.09
#> 9      9 Depression 5.35   5.27 0.22
#> 10    10 Depression 4.88   4.85 0.37
#> 11    11 Depression 4.36   4.43 0.31
#> 12    12 Depression 4.17   4.23 0.25

User-defined functions can also be used as the ‘statistic’. It’s passed using the statistic parameter:

#custom statistic
my_n <- function(xs) length(xs)

#calling the custom statistic
dstats(soils, pH, stats = c("my_n"))
#>   my_n
#> 1   48

User-defined functioned can also be passed alongside other statistics:

dstats(soils, pH, Group, Contour, Depth, stats=c("my_n","mean"))
#>    Group    Contour Depth my_n mean
#> 1      1        Top  0-10    4 5.33
#> 2      2        Top 10-30    4 4.85
#> 3      3        Top 30-60    4 4.20
#> 4      4        Top 60-90    4 3.89
#> 5      5      Slope  0-10    4 5.51
#> 6      6      Slope 10-30    4 5.28
#> 7      7      Slope 30-60    4 4.27
#> 8      8      Slope 60-90    4 3.93
#> 9      9 Depression  0-10    4 5.35
#> 10    10 Depression 10-30    4 4.88
#> 11    11 Depression 30-60    4 4.36
#> 12    12 Depression 60-90    4 4.17

Other optionalinputs into the function include the ‘na.rm’ parameter. When TRUE, NAs are removed. Default is TRUE

dstats(soils, pH, Depth, stats=c("mean","median","sd"),  na.rm=FALSE)
#>   Depth mean median   sd
#> 1  0-10 5.40   5.35 0.25
#> 2 10-30 5.00   4.85 0.60
#> 3 30-60 4.28   4.26 0.33
#> 4 60-90 4.00   3.95 0.20

Another optional input is digit, which is the number of decimal points to print. Default = 2.

dstats(soils, pH, Depth, stats=c("mean","median","sd"), na.rm=FALSE, digits=3)
#>   Depth  mean median    sd
#> 1  0-10 5.398  5.350 0.249
#> 2 10-30 5.004  4.850 0.597
#> 3 30-60 4.278  4.260 0.331
#> 4 60-90 3.998  3.945 0.203