Skip to contents

Calculate absorbance means with optional outlier removal

Usage

qp_calc_abs_mean(x, ignore_outliers = c("all", "standards", "samples", "none"))

# S3 method for data.frame
qp_calc_abs_mean(x, ignore_outliers = c("all", "standards", "samples", "none"))

# S3 method for list
qp_calc_abs_mean(x, ignore_outliers = c("all", "standards", "samples", "none"))

Arguments

x

A data.frame or list containing a data.frame named qp. See details.

ignore_outliers

Which sample types should have outliers ignored from their mean calculations? If .is_outlier column is supplied, this argument is ignored.

Value

The input tibble with an .is_outlier column and a .mean column

Details

Input data.frame must contain the following columns:

  • sample_type. Character. Must contain values either "standard" or "unknown"

  • index. Numeric. Denotes sample number.

  • .abs. Numeric. Contains absorbance values.

  • If a boolean .is_outlier is supplied, that will be used instead.

Examples

 library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union

abs <- expand.grid(
  sample_type = c("standard", "unknown"),
  index = 1:7,
  rep = 1:3
) |>
  dplyr::arrange(sample_type, index, rep)

abs$.abs <- abs(rnorm(nrow(abs), mean = abs$index))

# Selecting different subsets for outlier removal
qp_calc_abs_mean(abs, "none")
#>    sample_type index rep      .abs .is_outlier    .mean
#> 1     standard     1   1 0.4000435          NA 1.030875
#> 2     standard     1   2 1.2553171          NA 1.030875
#> 3     standard     1   3 1.4372636          NA 1.030875
#> 4     standard     2   1 1.9944287          NA 2.588131
#> 5     standard     2   2 2.6215527          NA 2.588131
#> 6     standard     2   3 3.1484116          NA 2.588131
#> 7     standard     3   1 1.1781823          NA 2.228886
#> 8     standard     3   2 2.7526747          NA 2.228886
#> 9     standard     3   3 2.7558004          NA 2.228886
#> 10    standard     4   1 3.7172946          NA 3.930859
#> 11    standard     4   2 3.4463006          NA 3.930859
#> 12    standard     4   3 4.6289820          NA 3.930859
#> 13    standard     5   1 7.0650249          NA 5.315487
#> 14    standard     5   2 3.3690106          NA 5.315487
#> 15    standard     5   3 5.5124269          NA 5.315487
#> 16    standard     6   1 4.1369885          NA 5.187458
#> 17    standard     6   2 5.4779875          NA 5.187458
#> 18    standard     6   3 5.9473981          NA 5.187458
#> 19    standard     7   1 7.5429963          NA 7.032359
#> 20    standard     7   2 6.0859252          NA 7.032359
#> 21    standard     7   3 7.4681544          NA 7.032359
#> 22     unknown     1   1 1.3629513          NA 1.135090
#> 23     unknown     1   2 0.3045435          NA 1.135090
#> 24     unknown     1   3 1.7377763          NA 1.135090
#> 25     unknown     2   1 3.8885049          NA 2.285071
#> 26     unknown     2   2 1.9025549          NA 2.285071
#> 27     unknown     2   3 1.0641526          NA 2.285071
#> 28     unknown     3   1 2.9840497          NA 2.214954
#> 29     unknown     3   2 2.1732110          NA 2.214954
#> 30     unknown     3   3 1.4876003          NA 2.214954
#> 31     unknown     4   1 4.9353632          NA 4.451846
#> 32     unknown     4   2 4.1764886          NA 4.451846
#> 33     unknown     4   3 4.2436855          NA 4.451846
#> 34     unknown     5   1 6.6235489          NA 5.533863
#> 35     unknown     5   2 5.1120381          NA 5.533863
#> 36     unknown     5   3 4.8660030          NA 5.533863
#> 37     unknown     6   1 4.0899125          NA 5.165743
#> 38     unknown     6   2 5.7207628          NA 5.165743
#> 39     unknown     6   3 5.6865540          NA 5.165743
#> 40     unknown     7   1 8.0673079          NA 7.166073
#> 41     unknown     7   2 7.0700349          NA 7.166073
#> 42     unknown     7   3 6.3608767          NA 7.166073

qp_calc_abs_mean(abs, "standards")
#>    sample_type index rep      .abs .is_outlier    .mean
#> 1     standard     1   1 0.4000435        TRUE 1.346290
#> 2     standard     1   2 1.2553171       FALSE 1.346290
#> 3     standard     1   3 1.4372636       FALSE 1.346290
#> 4     standard     2   1 1.9944287       FALSE 2.588131
#> 5     standard     2   2 2.6215527       FALSE 2.588131
#> 6     standard     2   3 3.1484116       FALSE 2.588131
#> 7     standard     3   1 1.1781823        TRUE 2.754238
#> 8     standard     3   2 2.7526747       FALSE 2.754238
#> 9     standard     3   3 2.7558004       FALSE 2.754238
#> 10    standard     4   1 3.7172946       FALSE 3.581798
#> 11    standard     4   2 3.4463006       FALSE 3.581798
#> 12    standard     4   3 4.6289820        TRUE 3.581798
#> 13    standard     5   1 7.0650249       FALSE 5.315487
#> 14    standard     5   2 3.3690106       FALSE 5.315487
#> 15    standard     5   3 5.5124269       FALSE 5.315487
#> 16    standard     6   1 4.1369885        TRUE 5.712693
#> 17    standard     6   2 5.4779875       FALSE 5.712693
#> 18    standard     6   3 5.9473981       FALSE 5.712693
#> 19    standard     7   1 7.5429963       FALSE 7.505575
#> 20    standard     7   2 6.0859252        TRUE 7.505575
#> 21    standard     7   3 7.4681544       FALSE 7.505575
#> 22     unknown     1   1 1.3629513          NA 1.135090
#> 23     unknown     1   2 0.3045435          NA 1.135090
#> 24     unknown     1   3 1.7377763          NA 1.135090
#> 25     unknown     2   1 3.8885049          NA 2.285071
#> 26     unknown     2   2 1.9025549          NA 2.285071
#> 27     unknown     2   3 1.0641526          NA 2.285071
#> 28     unknown     3   1 2.9840497          NA 2.214954
#> 29     unknown     3   2 2.1732110          NA 2.214954
#> 30     unknown     3   3 1.4876003          NA 2.214954
#> 31     unknown     4   1 4.9353632          NA 4.451846
#> 32     unknown     4   2 4.1764886          NA 4.451846
#> 33     unknown     4   3 4.2436855          NA 4.451846
#> 34     unknown     5   1 6.6235489          NA 5.533863
#> 35     unknown     5   2 5.1120381          NA 5.533863
#> 36     unknown     5   3 4.8660030          NA 5.533863
#> 37     unknown     6   1 4.0899125          NA 5.165743
#> 38     unknown     6   2 5.7207628          NA 5.165743
#> 39     unknown     6   3 5.6865540          NA 5.165743
#> 40     unknown     7   1 8.0673079          NA 7.166073
#> 41     unknown     7   2 7.0700349          NA 7.166073
#> 42     unknown     7   3 6.3608767          NA 7.166073

qp_calc_abs_mean(abs, "samples")
#>    sample_type index rep      .abs .is_outlier    .mean
#> 1     standard     1   1 0.4000435          NA 1.030875
#> 2     standard     1   2 1.2553171          NA 1.030875
#> 3     standard     1   3 1.4372636          NA 1.030875
#> 4     standard     2   1 1.9944287          NA 2.588131
#> 5     standard     2   2 2.6215527          NA 2.588131
#> 6     standard     2   3 3.1484116          NA 2.588131
#> 7     standard     3   1 1.1781823          NA 2.228886
#> 8     standard     3   2 2.7526747          NA 2.228886
#> 9     standard     3   3 2.7558004          NA 2.228886
#> 10    standard     4   1 3.7172946          NA 3.930859
#> 11    standard     4   2 3.4463006          NA 3.930859
#> 12    standard     4   3 4.6289820          NA 3.930859
#> 13    standard     5   1 7.0650249          NA 5.315487
#> 14    standard     5   2 3.3690106          NA 5.315487
#> 15    standard     5   3 5.5124269          NA 5.315487
#> 16    standard     6   1 4.1369885          NA 5.187458
#> 17    standard     6   2 5.4779875          NA 5.187458
#> 18    standard     6   3 5.9473981          NA 5.187458
#> 19    standard     7   1 7.5429963          NA 7.032359
#> 20    standard     7   2 6.0859252          NA 7.032359
#> 21    standard     7   3 7.4681544          NA 7.032359
#> 22     unknown     1   1 1.3629513       FALSE 1.550364
#> 23     unknown     1   2 0.3045435        TRUE 1.550364
#> 24     unknown     1   3 1.7377763       FALSE 1.550364
#> 25     unknown     2   1 3.8885049        TRUE 1.483354
#> 26     unknown     2   2 1.9025549       FALSE 1.483354
#> 27     unknown     2   3 1.0641526       FALSE 1.483354
#> 28     unknown     3   1 2.9840497       FALSE 2.214954
#> 29     unknown     3   2 2.1732110       FALSE 2.214954
#> 30     unknown     3   3 1.4876003       FALSE 2.214954
#> 31     unknown     4   1 4.9353632        TRUE 4.210087
#> 32     unknown     4   2 4.1764886       FALSE 4.210087
#> 33     unknown     4   3 4.2436855       FALSE 4.210087
#> 34     unknown     5   1 6.6235489        TRUE 4.989021
#> 35     unknown     5   2 5.1120381       FALSE 4.989021
#> 36     unknown     5   3 4.8660030       FALSE 4.989021
#> 37     unknown     6   1 4.0899125        TRUE 5.703658
#> 38     unknown     6   2 5.7207628       FALSE 5.703658
#> 39     unknown     6   3 5.6865540       FALSE 5.703658
#> 40     unknown     7   1 8.0673079       FALSE 7.166073
#> 41     unknown     7   2 7.0700349       FALSE 7.166073
#> 42     unknown     7   3 6.3608767       FALSE 7.166073

qp_calc_abs_mean(abs, "all")
#>    sample_type index rep      .abs .is_outlier    .mean
#> 1     standard     1   1 0.4000435        TRUE 1.346290
#> 2     standard     1   2 1.2553171       FALSE 1.346290
#> 3     standard     1   3 1.4372636       FALSE 1.346290
#> 4     standard     2   1 1.9944287       FALSE 2.588131
#> 5     standard     2   2 2.6215527       FALSE 2.588131
#> 6     standard     2   3 3.1484116       FALSE 2.588131
#> 7     standard     3   1 1.1781823        TRUE 2.754238
#> 8     standard     3   2 2.7526747       FALSE 2.754238
#> 9     standard     3   3 2.7558004       FALSE 2.754238
#> 10    standard     4   1 3.7172946       FALSE 3.581798
#> 11    standard     4   2 3.4463006       FALSE 3.581798
#> 12    standard     4   3 4.6289820        TRUE 3.581798
#> 13    standard     5   1 7.0650249       FALSE 5.315487
#> 14    standard     5   2 3.3690106       FALSE 5.315487
#> 15    standard     5   3 5.5124269       FALSE 5.315487
#> 16    standard     6   1 4.1369885        TRUE 5.712693
#> 17    standard     6   2 5.4779875       FALSE 5.712693
#> 18    standard     6   3 5.9473981       FALSE 5.712693
#> 19    standard     7   1 7.5429963       FALSE 7.505575
#> 20    standard     7   2 6.0859252        TRUE 7.505575
#> 21    standard     7   3 7.4681544       FALSE 7.505575
#> 22     unknown     1   1 1.3629513       FALSE 1.550364
#> 23     unknown     1   2 0.3045435        TRUE 1.550364
#> 24     unknown     1   3 1.7377763       FALSE 1.550364
#> 25     unknown     2   1 3.8885049        TRUE 1.483354
#> 26     unknown     2   2 1.9025549       FALSE 1.483354
#> 27     unknown     2   3 1.0641526       FALSE 1.483354
#> 28     unknown     3   1 2.9840497       FALSE 2.214954
#> 29     unknown     3   2 2.1732110       FALSE 2.214954
#> 30     unknown     3   3 1.4876003       FALSE 2.214954
#> 31     unknown     4   1 4.9353632        TRUE 4.210087
#> 32     unknown     4   2 4.1764886       FALSE 4.210087
#> 33     unknown     4   3 4.2436855       FALSE 4.210087
#> 34     unknown     5   1 6.6235489        TRUE 4.989021
#> 35     unknown     5   2 5.1120381       FALSE 4.989021
#> 36     unknown     5   3 4.8660030       FALSE 4.989021
#> 37     unknown     6   1 4.0899125        TRUE 5.703658
#> 38     unknown     6   2 5.7207628       FALSE 5.703658
#> 39     unknown     6   3 5.6865540       FALSE 5.703658
#> 40     unknown     7   1 8.0673079       FALSE 7.166073
#> 41     unknown     7   2 7.0700349       FALSE 7.166073
#> 42     unknown     7   3 6.3608767       FALSE 7.166073

# If an `.is_outlier` column is provided, that will be used instead:

abs$.is_outlier <- rep(c(TRUE, FALSE), length.out = nrow(abs))

qp_calc_abs_mean(abs)
#> Data has `.is_outlier` column, using that
#>    sample_type index rep      .abs .is_outlier    .mean
#> 1     standard     1   1 0.4000435        TRUE 1.255317
#> 2     standard     1   2 1.2553171       FALSE 1.255317
#> 3     standard     1   3 1.4372636        TRUE 1.255317
#> 4     standard     2   1 1.9944287       FALSE 2.571420
#> 5     standard     2   2 2.6215527        TRUE 2.571420
#> 6     standard     2   3 3.1484116       FALSE 2.571420
#> 7     standard     3   1 1.1781823        TRUE 2.752675
#> 8     standard     3   2 2.7526747       FALSE 2.752675
#> 9     standard     3   3 2.7558004        TRUE 2.752675
#> 10    standard     4   1 3.7172946       FALSE 4.173138
#> 11    standard     4   2 3.4463006        TRUE 4.173138
#> 12    standard     4   3 4.6289820       FALSE 4.173138
#> 13    standard     5   1 7.0650249        TRUE 3.369011
#> 14    standard     5   2 3.3690106       FALSE 3.369011
#> 15    standard     5   3 5.5124269        TRUE 3.369011
#> 16    standard     6   1 4.1369885       FALSE 5.042193
#> 17    standard     6   2 5.4779875        TRUE 5.042193
#> 18    standard     6   3 5.9473981       FALSE 5.042193
#> 19    standard     7   1 7.5429963        TRUE 6.085925
#> 20    standard     7   2 6.0859252       FALSE 6.085925
#> 21    standard     7   3 7.4681544        TRUE 6.085925
#> 22     unknown     1   1 1.3629513       FALSE 1.550364
#> 23     unknown     1   2 0.3045435        TRUE 1.550364
#> 24     unknown     1   3 1.7377763       FALSE 1.550364
#> 25     unknown     2   1 3.8885049        TRUE 1.902555
#> 26     unknown     2   2 1.9025549       FALSE 1.902555
#> 27     unknown     2   3 1.0641526        TRUE 1.902555
#> 28     unknown     3   1 2.9840497       FALSE 2.235825
#> 29     unknown     3   2 2.1732110        TRUE 2.235825
#> 30     unknown     3   3 1.4876003       FALSE 2.235825
#> 31     unknown     4   1 4.9353632        TRUE 4.176489
#> 32     unknown     4   2 4.1764886       FALSE 4.176489
#> 33     unknown     4   3 4.2436855        TRUE 4.176489
#> 34     unknown     5   1 6.6235489       FALSE 5.744776
#> 35     unknown     5   2 5.1120381        TRUE 5.744776
#> 36     unknown     5   3 4.8660030       FALSE 5.744776
#> 37     unknown     6   1 4.0899125        TRUE 5.720763
#> 38     unknown     6   2 5.7207628       FALSE 5.720763
#> 39     unknown     6   3 5.6865540        TRUE 5.720763
#> 40     unknown     7   1 8.0673079       FALSE 7.214092
#> 41     unknown     7   2 7.0700349        TRUE 7.214092
#> 42     unknown     7   3 6.3608767       FALSE 7.214092