R Under development (unstable) (2024-09-02 r87090 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library("matrixStats") > > X <- matrix(rnorm(20 * 6), nrow = 20, ncol = 6) > rownames(X) <- LETTERS[1:nrow(X)] > colnames(X) <- letters[1:ncol(X)] > print(X) a b c d e f A -0.037561278 -0.69124401 -1.2063616 -0.22803304 -1.9415155 0.27527719 B -1.232901326 0.01424212 1.2198552 -1.37814250 -0.8454036 -0.91046625 C -0.068305128 -1.50578568 -1.3525646 0.61892390 0.6313323 0.04709732 D 0.525548360 -0.74401884 0.8519266 1.16323288 -0.4612961 3.17512020 E 0.679384754 1.11143512 -1.3306664 -0.35418465 0.5305731 2.25029148 F -0.303877260 -0.44835610 -2.1947313 1.25992466 1.2981786 -0.06754025 G -0.059028476 -0.31021009 -0.1381598 0.96119890 1.5199067 2.40029609 H 0.108932521 0.60391030 0.4813562 -1.09999078 -0.3805183 -0.45520633 I -0.199218808 -1.04129704 -0.5407357 -0.60071681 -0.9272650 0.59987885 J 0.310483360 0.88530572 0.3088392 1.85825406 0.3995440 -1.07248634 K 0.109067262 0.69708946 1.2524319 1.22819315 -0.8155346 -0.91589587 L 1.221403423 -1.17091945 -1.1006213 0.84053599 0.8775793 0.16322782 M 0.181593856 0.53993876 -0.2714504 0.81258605 -0.3399437 0.06337288 N -0.367111000 1.44388333 0.3156711 -0.09972671 0.9876879 -0.83412585 O 0.360922005 1.83701578 0.2704398 -0.58814822 -2.0017957 -1.17796138 P -1.967493090 0.04067767 -0.4460604 -0.27434366 0.4427933 0.36703217 Q 0.515302971 0.17585139 -0.7364696 0.96663217 1.4059317 0.75661631 R 0.005360476 1.30609581 -1.2444519 -1.19125241 -0.4679042 0.31810214 S -0.727555454 -0.52268488 1.6107651 1.04167640 -0.6702601 0.25725743 T 0.063628983 0.27701368 -0.3853316 0.74196136 1.2018924 0.55369820 > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # Apply rowMeans() for 3 sets of 2 columns > # - - - - - - - - - - - - - - - - - - - - - - - - - - > nbr_of_sets <- 3L > S <- matrix(1:ncol(X), ncol = nbr_of_sets) > colnames(S) <- sprintf("s%d", 1:nbr_of_sets) > print(S) s1 s2 s3 [1,] 1 3 5 [2,] 2 4 6 > > Z <- rowAvgsPerColSet(X, S = S) > print(Z) s1 s2 s3 A -0.36440264 -0.71719731 -0.83311916 B -0.60932960 -0.07914366 -0.87793495 C -0.78704540 -0.36682035 0.33921479 D -0.10923524 1.00757976 1.35691204 E 0.89540994 -0.84242552 1.39043229 F -0.37611668 -0.46740334 0.61531918 G -0.18461928 0.41151954 1.96010141 H 0.35642141 -0.30931727 -0.41786230 I -0.62025793 -0.57072623 -0.16369307 J 0.59789454 1.08354662 -0.33647120 K 0.40307836 1.24031251 -0.86571524 L 0.02524199 -0.13004266 0.52040356 M 0.36076631 0.27056780 -0.13828541 N 0.53838616 0.10797219 0.07678101 O 1.09896889 -0.15885422 -1.58987852 P -0.96340771 -0.36020203 0.40491275 Q 0.34557718 0.11508128 1.08127401 R 0.65572814 -1.21785217 -0.07490102 S -0.62512017 1.32622074 -0.20650134 T 0.17032133 0.17831485 0.87779528 > > # Validation > Z0 <- cbind(s1 = rowMeans(X[, 1:2]), s2 = rowMeans(X[, 3:4]), + s3 = rowMeans(X[, 5:6])) > stopifnot(identical(drop(Z), Z0)) > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # Apply colMeans() for 5 sets of 4 rows > # - - - - - - - - - - - - - - - - - - - - - - - - - - > nbr_of_sets <- 5L > S <- matrix(1:nrow(X), ncol = nbr_of_sets) > colnames(S) <- sprintf("s%d", 1:nbr_of_sets) > print(S) s1 s2 s3 s4 s5 [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20 > > Z <- colAvgsPerRowSet(X, S = S) > print(Z) a b c d e f s1 -0.20330484 -0.7317016 -0.12178608 0.04399531 -0.6542208 0.6467571 s2 0.10635288 0.2391948 -0.79555033 0.19173703 0.7420350 1.0319602 s3 0.36043381 -0.1574553 -0.02002148 0.83156659 -0.1164191 -0.3063189 s4 -0.44802206 0.9653789 -0.03284999 -0.03740814 -0.2278145 -0.3954205 s5 -0.03581576 0.3090690 -0.18887203 0.38975438 0.3674149 0.4714185 > > # Validation > Z0 <- rbind(s1 = colMeans(X[1:4, ]), s2 = colMeans(X[5:8, ]), + s3 = colMeans(X[9:12, ]), s4 = colMeans(X[13:16, ]), + s5 = colMeans(X[17:20, ])) > stopifnot(identical(drop(Z), Z0)) > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # When there is only one "complete" set > # - - - - - - - - - - - - - - - - - - - - - - - - - - > nbr_of_sets <- 1L > S <- matrix(1:ncol(X), ncol = nbr_of_sets) > colnames(S) <- sprintf("s%d", 1:nbr_of_sets) > print(S) s1 [1,] 1 [2,] 2 [3,] 3 [4,] 4 [5,] 5 [6,] 6 > > Z <- rowAvgsPerColSet(X, S = S, FUN = rowMeans) > print(Z) s1 A -0.63823970 B -0.52213607 C -0.27155032 D 0.75175219 E 0.48113890 F -0.07606695 G 0.72900056 H -0.12358605 I -0.45155908 J 0.44832332 K 0.25922521 L 0.13853429 M 0.16434957 N 0.24104646 O -0.21658795 P -0.30623233 Q 0.51397749 R -0.21234168 S 0.16486641 T 0.40881049 > > Z0 <- rowMeans(X) > stopifnot(identical(drop(Z), Z0)) > > > nbr_of_sets <- 1L > S <- matrix(1:nrow(X), ncol = nbr_of_sets) > colnames(S) <- sprintf("s%d", 1:nbr_of_sets) > print(S) s1 [1,] 1 [2,] 2 [3,] 3 [4,] 4 [5,] 5 [6,] 6 [7,] 7 [8,] 8 [9,] 9 [10,] 10 [11,] 11 [12,] 12 [13,] 13 [14,] 14 [15,] 15 [16,] 16 [17,] 17 [18,] 18 [19,] 19 [20,] 20 > > Z <- colAvgsPerRowSet(X, S = S, FUN = colMeans) > print(Z) a b c d e f s1 -0.04407119 0.1248972 -0.231816 0.283929 0.02219912 0.2896793 > > Z0 <- colMeans(X) > stopifnot(identical(drop(Z), Z0)) > > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # Use weights > # - - - - - - - - - - - - - - - - - - - - - - - - - - > nbr_of_sets <- 3L > S <- matrix(1:ncol(X), ncol = nbr_of_sets) > colnames(S) <- sprintf("s%d", 1:nbr_of_sets) > print(S) s1 s2 s3 [1,] 1 3 5 [2,] 2 4 6 > > W <- matrix(runif(length(X)), nrow = nrow(X), ncol = ncol(X)) > Z1 <- rowAvgsPerColSet(X, W = W, S = S, FUN = rowWeightedMeans) > print(Z1) s1 s2 s3 A -0.36440264 -0.71719731 -0.83311916 B -0.60932960 -0.07914366 -0.87793495 C -0.78704540 -0.36682035 0.33921479 D -0.10923524 1.00757976 1.35691204 E 0.89540994 -0.84242552 1.39043229 F -0.37611668 -0.46740334 0.61531918 G -0.18461928 0.41151954 1.96010141 H 0.35642141 -0.30931727 -0.41786230 I -0.62025793 -0.57072623 -0.16369307 J 0.59789454 1.08354662 -0.33647120 K 0.40307836 1.24031251 -0.86571524 L 0.02524199 -0.13004266 0.52040356 M 0.36076631 0.27056780 -0.13828541 N 0.53838616 0.10797219 0.07678101 O 1.09896889 -0.15885422 -1.58987852 P -0.96340771 -0.36020203 0.40491275 Q 0.34557718 0.11508128 1.08127401 R 0.65572814 -1.21785217 -0.07490102 S -0.62512017 1.32622074 -0.20650134 T 0.17032133 0.17831485 0.87779528 > Z2 <- colAvgsPerRowSet(X, W = W, S = S, FUN = colWeightedMeans) > print(Z2) a b c d e f s1 -0.6352313 -0.3385009 0.006746806 -0.8030878 -1.39345957 -0.3175945 s2 0.2286216 -1.1249023 -0.250318975 0.8910784 0.08501807 1.6111088 s3 0.1877537 0.3315395 -1.762698864 0.4528700 0.91437584 1.0913756 > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # Result should always be a matrix, including when nrow(X) <= 1 > # (https://github.com/HenrikBengtsson/matrixStats/issues/108) > # - - - - - - - - - - - - - - - - - - - - - - - - - - > X <- matrix(1:3, nrow = 1L, ncol = 3L) > S <- matrix(1, nrow = 1L, ncol = 1L) > Z1 <- rowAvgsPerColSet(X, S = S) > stopifnot(is.matrix(Z1)) > Z2 <- rowAvgsPerColSet(X, S = S, rows = 0) > stopifnot(is.matrix(Z2)) > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # Works with many, one or zero columns / rows > # (https://github.com/HenrikBengtsson/matrixStats/issues/172) > # - - - - - - - - - - - - - - - - - - - - - - - - - - > S <- cbind(1:2, 3:4, 5:6) > X <- matrix(rnorm(2 * 6), nrow = 6, ncol = 2) > Z2 <- colAvgsPerRowSet(X, S = S, FUN = colSums2) > Z2_ref <- rbind(colSums2(X[S[,1], ,drop=FALSE]), + colSums2(X[S[,2], ,drop=FALSE]), + colSums2(X[S[,3], ,drop=FALSE])) > stopifnot(identical(Z2, Z2_ref)) > X <- matrix(rnorm(6), nrow = 6, ncol = 1) > Z1 <- colAvgsPerRowSet(X, S = S, FUN = colSums2) > Z1_ref <- rbind(colSums2(X[S[,1], ,drop=FALSE]), + colSums2(X[S[,2], ,drop=FALSE]), + colSums2(X[S[,3], ,drop=FALSE])) > stopifnot(identical(Z1, Z1_ref)) > X <- matrix(numeric(0), nrow = 6, ncol = 0) > Z0 <- colAvgsPerRowSet(X, S = S, FUN = colSums2) > Z0_ref <- matrix(numeric(0), nrow = ncol(S), ncol = 0) > stopifnot(identical(Z0, unname(Z0_ref))) > > > S <- rbind(1:4, 5:8) > X <- matrix(rnorm(n = 2 * 8), nrow = 2, ncol = 8) > Z2 <- rowAvgsPerColSet(X, S = S, FUN = rowMeans2) > Z2_ref <- cbind(rowMeans2(X[,S[,1],drop=FALSE]), + rowMeans2(X[,S[,2],drop=FALSE]), + rowMeans2(X[,S[,3],drop=FALSE]), + rowMeans2(X[,S[,4],drop=FALSE])) > stopifnot(identical(Z2, Z2_ref)) > X <- matrix(rnorm(n = 8), nrow = 1, ncol = 8) > Z1 <- rowAvgsPerColSet(X, S = S, FUN = rowMeans2) > Z1_ref <- cbind(rowMeans2(X[,S[,1],drop=FALSE]), + rowMeans2(X[,S[,2],drop=FALSE]), + rowMeans2(X[,S[,3],drop=FALSE]), + rowMeans2(X[,S[,4],drop=FALSE])) > stopifnot(identical(Z1, Z1_ref)) > X <- matrix(numeric(0), nrow = 0, ncol = 8) > Z0 <- rowAvgsPerColSet(X, S = S, FUN = rowMeans2) > Z0_ref <- matrix(numeric(0), nrow = 0, ncol = ncol(S)) > stopifnot(identical(Z0, Z0_ref)) > > proc.time() user system elapsed 0.23 0.07 0.28