R version 4.4.0 beta (2024-04-09 r86391 ucrt) -- "Puppy Cup" 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.4601970 1.18096637 0.50695326 1.21799820 -1.050544474 0.04766031 B -1.4135069 -0.39623654 1.78183307 -0.48861682 -0.353168750 0.04757229 C 0.5827100 0.15462441 -0.35407939 -1.59821245 0.388370213 2.10963205 D -0.3784000 -0.34789307 0.49324862 0.20358473 -0.951823525 -0.16685407 E -0.7376988 -0.88173474 0.08750102 0.66496598 -0.456406981 0.78358401 F 1.2362092 -0.04042119 0.19862756 -0.80398923 -0.410598840 -1.13301056 G 0.8003457 -1.27513561 0.25307281 1.10848994 1.499147785 -0.55826663 H 0.2350434 1.51520991 1.05433555 -0.40764986 0.396215380 -0.30948886 I 0.7396252 2.25697884 -0.15891702 0.61527697 -0.292836533 -0.69289329 J 1.3506210 1.89613959 1.33884933 -0.14364821 1.211510334 0.96742895 K -0.7661833 0.92593308 -1.29955783 0.17019272 0.359593226 -0.17260413 L -2.3013159 -0.92145672 -0.59108210 1.94413511 1.456115413 -1.85454809 M 1.5793125 -0.15931298 0.77412580 -0.59009826 0.638614208 0.53543674 N -2.0047220 1.08965581 0.07662574 -0.42453909 0.868832575 0.40334807 O 1.3078225 0.25645397 1.98567958 0.01785335 0.238411622 -1.61499938 P -0.1769880 -0.42865134 0.68768936 0.22813139 0.714634527 -0.43728867 Q -1.5292416 0.56552346 0.79646461 -1.52707938 0.210926116 -0.12652271 R 0.2900066 0.73640879 0.48469125 -0.11613439 0.005918816 0.89326424 S -0.6326908 -2.04763814 -1.58965779 0.54455217 0.746461491 -0.57487663 T -0.2272158 -0.87650278 -0.32165830 -0.11563874 1.178244243 0.46906662 > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # 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.82058167 0.86247573 -0.50144208 B -0.90487170 0.64660813 -0.15279823 C 0.36866719 -0.97614592 1.24900113 D -0.36314654 0.34841667 -0.55933880 E -0.80971675 0.37623350 0.16358851 F 0.59789400 -0.30268083 -0.77180470 G -0.23739497 0.68078137 0.47044058 H 0.87512667 0.32334285 0.04336326 I 1.49830203 0.22817998 -0.49286491 J 1.62338029 0.59760056 1.08946964 K 0.07987489 -0.56468255 0.09349455 L -1.61138633 0.67652650 -0.19921634 M 0.70999975 0.09201377 0.58702547 N -0.45753310 -0.17395667 0.63609032 O 0.78213821 1.00176646 -0.68829388 P -0.30281969 0.45791037 0.13867293 Q -0.48185905 -0.36530738 0.04220170 R 0.51320770 0.18427843 0.44959153 S -1.34016449 -0.52255281 0.08579243 T -0.55185930 -0.21864852 0.82365543 > > # 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.1872500 0.1478653 0.6069889 -0.1663116 -0.4917916 0.5095026 s2 0.3834749 -0.1705204 0.3983842 0.1404542 0.2570893 -0.3042955 s3 -0.2443133 1.0393987 -0.1776769 0.6464891 0.6835956 -0.4381541 s4 0.1763562 0.1895364 0.8810301 -0.1921632 0.6151232 -0.2783758 s5 -0.5247854 -0.4055522 -0.1575401 -0.3035751 0.5353877 0.1652329 > > # 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.393871773 B -0.137020601 C 0.213840802 D -0.191356223 E -0.089964913 F -0.158863843 G 0.304608992 H 0.413944258 I 0.411205700 J 1.103483496 K -0.130437704 L -0.378025390 M 0.463012999 N 0.001533518 O 0.365203599 P 0.097921202 Q -0.268321576 R 0.382359219 S -0.592308290 T 0.017715872 > > 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.07930351 0.1601456 0.3102373 0.02497871 0.3198808 -0.06921799 > > 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.82058167 0.86247573 -0.50144208 B -0.90487170 0.64660813 -0.15279823 C 0.36866719 -0.97614592 1.24900113 D -0.36314654 0.34841667 -0.55933880 E -0.80971675 0.37623350 0.16358851 F 0.59789400 -0.30268083 -0.77180470 G -0.23739497 0.68078137 0.47044058 H 0.87512667 0.32334285 0.04336326 I 1.49830203 0.22817998 -0.49286491 J 1.62338029 0.59760056 1.08946964 K 0.07987489 -0.56468255 0.09349455 L -1.61138633 0.67652650 -0.19921634 M 0.70999975 0.09201377 0.58702547 N -0.45753310 -0.17395667 0.63609032 O 0.78213821 1.00176646 -0.68829388 P -0.30281969 0.45791037 0.13867293 Q -0.48185905 -0.36530738 0.04220170 R 0.51320770 0.18427843 0.44959153 S -1.34016449 -0.52255281 0.08579243 T -0.55185930 -0.21864852 0.82365543 > Z2 <- colAvgsPerRowSet(X, W = W, S = S, FUN = colWeightedMeans) > print(Z2) a b c d e f s1 -0.4766549 0.39236492 1.14439317 0.36469069 -0.7018566 0.0476163 s2 0.1021550 -0.09663433 0.06958462 -0.69731386 -0.2817267 0.9713890 s3 0.2492552 -0.46107796 0.14306429 -0.06951162 -0.4335029 -0.1747133 > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # 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.14 0.01 0.15