R Under development (unstable) (2025-01-06 r87534 ucrt) -- "Unsuffered Consequences" Copyright (C) 2025 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.4991336361 -0.23460315 1.2320736 0.15720768 -0.2317038 1.1273737 B -0.7715552049 0.18250494 -2.2089653 -0.26672639 1.9082003 0.5207806 C -0.1808134523 0.75302928 0.8266678 -0.85859290 -0.9328480 -0.9770030 D 1.7551978164 0.38447608 2.7534627 0.90541871 -0.2920476 0.4169951 E 0.0001493494 -0.37296713 -1.2883389 -0.21217030 2.5788561 0.2566557 F 1.8724399326 -0.31296212 0.2741073 -2.12527846 -0.4466167 -0.8996720 G 0.7541903589 -0.87191208 0.3444010 -0.07563953 1.2138425 0.6528119 H 0.6492457501 1.54093224 0.2308988 -0.02967572 0.7598470 -0.1090788 I 1.7565004969 -0.46985094 0.9544676 -0.59629219 0.9674942 1.3119348 J -0.2128127949 -2.32818279 -1.1481755 -0.42745732 -1.8301145 2.2393533 K 0.2956930309 -0.38262100 -0.7855258 -0.21961388 1.2304600 0.3811510 L 0.0932151398 -0.14014343 -0.1703359 1.26723328 -0.4219542 -0.2186315 M -1.8736882695 0.01633856 -0.3906845 1.23809484 0.5421143 -0.7132700 N -1.2954481359 -2.34308318 0.8645440 0.93804991 -0.9819006 0.1206463 O 2.3817490497 -0.77329039 -0.6879919 -0.62080546 -0.5692041 0.8434836 P -2.1346292931 -1.51738165 0.3556764 -0.08903128 -0.4975366 0.6797001 Q 0.0221515534 1.22890602 0.4594616 1.15303636 0.5030700 -1.0716980 R 0.6386445511 1.55393620 0.4800185 -2.05178032 1.2882436 0.3230892 S 0.8994222998 -0.23534761 0.1839955 0.32883096 0.2205672 0.4596049 T 0.1213730080 0.94625567 0.9688444 -0.51929720 -0.8770811 0.3183382 > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # 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.13226524 0.69464063 0.44783492 B -0.29452513 -1.23784586 1.21449047 C 0.28610791 -0.01596255 -0.95492550 D 1.06983695 1.82944071 0.06247374 E -0.18640889 -0.75025462 1.41775586 F 0.77973891 -0.92558560 -0.67314434 G -0.05886086 0.13438074 0.93332718 H 1.09508900 0.10061154 0.32538414 I 0.64332478 0.17908771 1.13971452 J -1.27049779 -0.78781644 0.20461941 K -0.04346399 -0.50256985 0.80580547 L -0.02346415 0.54844871 -0.32029284 M -0.92867485 0.42370516 -0.08557787 N -1.81926566 0.90129696 -0.43062712 O 0.80422933 -0.65439869 0.13713971 P -1.82600547 0.13332254 0.09108176 Q 0.62552879 0.80624896 -0.28431401 R 1.09629037 -0.78588089 0.80566639 S 0.33203734 0.25641325 0.34008605 T 0.53381434 0.22477360 -0.27937141 > > # 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.3254907 0.27135178 0.65080969 -0.015673223 0.11290022 0.272036597 s2 0.8190063 -0.00422727 -0.10973297 -0.610691002 1.02648222 -0.024820804 s3 0.4831490 -0.83019954 -0.28739240 0.005967474 -0.01352861 0.928451882 s4 -0.7305042 -1.15435416 0.03538598 0.366577002 -0.37663176 0.232640004 s5 0.4203979 0.87343757 0.52308000 -0.272302548 0.28369993 0.007333588 > > # 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.42491360 B -0.10596018 C -0.22826004 D 0.98725046 E 0.16036412 F -0.27299701 G 0.33628235 H 0.50702822 I 0.65404234 J -0.61789827 K 0.08659055 L 0.06823058 M -0.19684919 N -0.44953194 O 0.09565679 P -0.53386706 Q 0.38248791 R 0.37202529 S 0.30951221 T 0.15973884 > > 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.2635079 -0.1687983 0.1624301 -0.1052245 0.2065844 0.2831283 > > 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.13226524 0.69464063 0.44783492 B -0.29452513 -1.23784586 1.21449047 C 0.28610791 -0.01596255 -0.95492550 D 1.06983695 1.82944071 0.06247374 E -0.18640889 -0.75025462 1.41775586 F 0.77973891 -0.92558560 -0.67314434 G -0.05886086 0.13438074 0.93332718 H 1.09508900 0.10061154 0.32538414 I 0.64332478 0.17908771 1.13971452 J -1.27049779 -0.78781644 0.20461941 K -0.04346399 -0.50256985 0.80580547 L -0.02346415 0.54844871 -0.32029284 M -0.92867485 0.42370516 -0.08557787 N -1.81926566 0.90129696 -0.43062712 O 0.80422933 -0.65439869 0.13713971 P -1.82600547 0.13332254 0.09108176 Q 0.62552879 0.80624896 -0.28431401 R 1.09629037 -0.78588089 0.80566639 S 0.33203734 0.25641325 0.34008605 T 0.53381434 0.22477360 -0.27937141 > Z2 <- colAvgsPerRowSet(X, W = W, S = S, FUN = colWeightedMeans) > print(Z2) a b c d e f s1 -0.1362108 -0.02604911 -0.4884459 -0.05475935 0.8382483 0.8240771 s2 0.7871922 0.56875268 1.7900653 0.02341291 -0.6124478 -0.2800039 s3 0.9362946 -0.34296462 -0.5071158 -1.16872438 1.0661197 -0.3215082 > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # 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.12 0.04 0.15