Estimates quantiles for each row (column) in a matrix.
rowQuantiles(x, rows = NULL, cols = NULL, probs = seq(from = 0, to = 1,
by = 0.25), na.rm = FALSE, type = 7L, digits = 7L, ...,
useNames = TRUE, drop = TRUE)
colQuantiles(x, rows = NULL, cols = NULL, probs = seq(from = 0, to = 1,
by = 0.25), na.rm = FALSE, type = 7L, digits = 7L, ...,
useNames = TRUE, drop = TRUE)A vector indicating subset of rows to
operate over. If NULL, no subsetting is done.
A vector indicating subset of columns to
operate over. If NULL, no subsetting is done.
If TRUE, missing values are
excluded.
An integer specifying the type of estimator.
See quantile for more details.
An integer specifying the precision of
the formatted percentages. Not used when `useNames = FALSE`.
In **matrixStats** (< 0.63.0), the default used to be
`max(2L, getOption("digits"))` in line with R (< 4.1.0).
Additional arguments passed to quantile.
If TRUE (default), names
attributes of the result are set, otherwise not.
If TRUE, singleton dimensions in the result are dropped, otherwise not.
Returns a NxJ (KxJ) matrix, where N (K) is the
number of rows (columns) for which the J quantiles are calculated.
The return type is either integer or numeric depending on type.
set.seed(1)
x <- matrix(rnorm(50 * 40), nrow = 50, ncol = 40)
str(x)
#> num [1:50, 1:40] -0.626 0.184 -0.836 1.595 0.33 ...
probs <- c(0.25, 0.5, 0.75)
# Row quantiles
q <- rowQuantiles(x, probs = probs)
print(q)
#> 25% 50% 75%
#> [1,] -0.6218885 0.106762508 0.5512421
#> [2,] -0.3049272 0.056473676 0.7057890
#> [3,] -0.9231354 -0.118361292 0.7422711
#> [4,] -0.9322740 -0.207686499 0.3216682
#> [5,] -0.7084443 -0.035489532 0.6621024
#> [6,] -0.9664491 -0.216591179 0.6740367
#> [7,] -1.0384055 0.042690771 0.6611677
#> [8,] -0.6330825 -0.126332443 0.6960917
#> [9,] -1.0094086 -0.041113665 0.6791968
#> [10,] -0.6327419 0.054660577 0.6222087
#> [11,] -0.5645887 -0.041477859 0.3713240
#> [12,] -0.2766925 0.110955064 0.7428664
#> [13,] -0.8453155 -0.334769330 0.5554439
#> [14,] -0.8897809 -0.324080709 0.3301178
#> [15,] -0.7251620 -0.188349538 0.3535435
#> [16,] -0.4797823 0.091562203 0.9726725
#> [17,] -0.6200479 -0.262020284 0.6356199
#> [18,] -0.9034017 -0.140455912 0.7528919
#> [19,] -0.7707722 -0.152329236 0.1729031
#> [20,] -0.3200883 0.302574383 1.2132039
#> [21,] -0.5291880 -0.214782932 0.5513092
#> [22,] -0.5356002 0.284290389 0.7989148
#> [23,] -0.5700878 0.149494637 0.8077412
#> [24,] -0.7503936 -0.077770110 0.5309214
#> [25,] -0.4267110 0.263799916 0.7293052
#> [26,] -0.9591707 -0.137898692 0.3078452
#> [27,] -1.0171397 -0.150011032 0.6103426
#> [28,] -0.2687164 0.085033622 0.7435987
#> [29,] -0.7860127 -0.286445627 0.5151106
#> [30,] -0.3577578 -0.022684187 0.5654628
#> [31,] -0.9519151 -0.538929463 0.4145123
#> [32,] -0.6973686 -0.118983171 0.9168207
#> [33,] -0.8027962 -0.204718112 0.4979713
#> [34,] -0.9273236 -0.141973088 0.4361869
#> [35,] -0.4532102 0.365402869 0.7459632
#> [36,] -0.4184018 0.227594464 0.7615129
#> [37,] -0.6367977 0.088614257 0.9296158
#> [38,] -0.4039084 -0.010658183 0.7876346
#> [39,] -0.3561294 0.373172018 1.0347134
#> [40,] -0.7034831 -0.005856479 0.5258082
#> [41,] -0.6183522 -0.064312777 0.7153803
#> [42,] -0.4235112 0.125571403 0.6848875
#> [43,] -0.5556839 0.124678003 0.5593517
#> [44,] -0.5123758 0.439573098 0.8486753
#> [45,] -1.0042770 -0.060667550 0.9481150
#> [46,] -1.0340398 -0.310771412 0.1371124
#> [47,] -0.8865014 0.142643098 0.9203617
#> [48,] -0.7968222 -0.172298103 0.6482758
#> [49,] -1.2293839 -0.139233624 0.5217433
#> [50,] -0.6553804 -0.262142026 0.4742108
q_0 <- apply(x, MARGIN = 1, FUN = quantile, probs = probs)
stopifnot(all.equal(q_0, t(q)))
# Column IQRs
q <- colQuantiles(x, probs = probs)
print(q)
#> 25% 50% 75%
#> [1,] -0.3720646 0.129104154 0.7279844
#> [2,] -0.5721162 0.113797331 0.6065313
#> [3,] -0.6469564 -0.246846356 0.2694256
#> [4,] -0.7041278 -0.055939482 0.8196898
#> [5,] -0.6089229 -0.029054330 0.6243079
#> [6,] -0.4185202 0.146116784 0.7594495
#> [7,] -0.4365107 0.091022110 0.8118479
#> [8,] -0.6197929 -0.032415219 0.6618606
#> [9,] -0.6727637 -0.273421020 0.6330157
#> [10,] -0.8825293 -0.011512761 0.5805665
#> [11,] -0.6381655 0.014950044 0.4831043
#> [12,] -0.7140804 -0.152388294 0.8106097
#> [13,] -0.7242296 -0.251628169 0.5387204
#> [14,] -1.0515879 -0.189977384 0.5571419
#> [15,] -0.9282411 -0.084192983 1.0093225
#> [16,] -0.8448598 -0.011103461 0.8216741
#> [17,] -0.5906504 0.161907795 0.7795111
#> [18,] -0.6499464 -0.059279451 0.5886333
#> [19,] -0.6094663 0.214748047 0.7317293
#> [20,] -0.7037132 0.022335956 0.5108679
#> [21,] -0.8504756 0.108484526 0.7602824
#> [22,] -0.7317770 0.007196516 0.7568682
#> [23,] -0.8515848 -0.319332762 0.5102586
#> [24,] -0.6366325 -0.249590203 0.6963329
#> [25,] -0.4791826 0.007958062 0.7657119
#> [26,] -0.4125492 0.291176656 1.0945407
#> [27,] -0.8272824 -0.267580711 0.3454911
#> [28,] -0.8544896 -0.197365516 0.4950755
#> [29,] -0.5364474 0.169648975 0.9752908
#> [30,] -0.6411633 0.057236706 0.5722226
#> [31,] -0.4458641 -0.018247105 0.7952345
#> [32,] -0.8358661 0.071609588 0.5724964
#> [33,] -0.5417449 0.124909830 0.5086025
#> [34,] -0.8415534 -0.215944585 0.5483784
#> [35,] -0.7080412 -0.017766239 1.0980467
#> [36,] -0.9491461 -0.256300105 0.4286593
#> [37,] -0.2463305 0.355645772 0.9952349
#> [38,] -0.5823101 -0.221579869 0.6592843
#> [39,] -0.8941790 -0.001721324 0.3406492
#> [40,] -0.6990172 -0.038793345 0.8817002
q_0 <- apply(x, MARGIN = 2, FUN = quantile, probs = probs)
stopifnot(all.equal(q_0, t(q)))