R Under development (unstable) (2023-11-06 r85483 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 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.53832621 -0.60751283 0.12222754 0.6082800 -0.55182660 -0.29945937 B 0.37659033 -0.83958386 -0.62973858 0.5064876 0.46336655 0.90943236 C 0.23040458 -0.16667898 -0.97807634 -1.9546061 0.78302226 -0.15681891 D 0.16784685 -0.05209025 0.05750649 -0.5111902 0.66674934 1.12159394 E 0.53642240 -0.07557053 0.86682209 -1.0155105 -0.38618552 0.10223245 F 1.22341122 2.05529394 1.48102806 0.3428609 0.86114951 0.48878455 G -0.53794675 0.38331227 1.27364158 1.8446877 2.32575842 0.06158827 H 0.40652291 0.05188388 1.81843027 0.7013120 -0.43487829 0.97088339 I 0.49164170 -0.18030748 -2.37052240 -0.4960142 -1.16380776 0.89591630 J -0.04047518 1.67342821 0.55755961 0.5396816 1.29654379 -0.93426425 K 0.28691498 -1.31848365 -1.94561467 -0.4728998 -0.20395435 0.39724381 L -0.38630693 -0.13708776 -0.21663615 -0.1660457 -0.01392091 0.04732559 M -0.16347043 -0.91513198 -1.69331279 0.6879835 -0.07565013 -0.33860503 N -0.05609498 0.02350046 -0.29584680 2.1912151 1.82507885 -3.37126356 O 0.62755790 -1.87196376 -0.74491287 0.1294873 -0.90427816 2.12438767 P -0.74003434 -0.96760310 1.35108943 -0.1279612 0.94010670 -0.14830273 Q 0.45559861 -1.42955626 -1.07833090 2.6958685 -1.18101339 0.98466951 R -0.94008934 -0.04285923 0.10622706 -0.3418189 -0.04787626 0.20980509 S -0.27618081 0.47143778 -0.48920036 -0.5480233 -0.62528006 -1.30680008 T 1.00020499 0.80024769 -0.33529900 -0.2188805 0.28940839 0.18712182 > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # 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.03459331 0.36525377 -0.42564299 B -0.23149676 -0.06162550 0.68639945 C 0.03186280 -1.46634124 0.31310167 D 0.05787830 -0.22684184 0.89417164 E 0.23042593 -0.07434419 -0.14197653 F 1.63935258 0.91194450 0.67496703 G -0.07731724 1.55916463 1.19367335 H 0.22920340 1.25987114 0.26800255 I 0.15566711 -1.43326829 -0.13394573 J 0.81647651 0.54862062 0.18113977 K -0.51578433 -1.20925722 0.09664473 L -0.26169734 -0.19134091 0.01670234 M -0.53930121 -0.50266463 -0.20712758 N -0.01629726 0.94768413 -0.77309236 O -0.62220293 -0.30771280 0.61005475 P -0.85381872 0.61156412 0.39590198 Q -0.48697882 0.80876879 -0.09817194 R -0.49147429 -0.11779593 0.08096442 S 0.09762849 -0.51861184 -0.96604007 T 0.90022634 -0.27708973 0.23826510 > > # 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.32829199 -0.416466479 -0.3570202 -0.3377572 0.34032789 0.39368700 s2 0.40710245 0.603729890 1.3599805 0.4683375 0.59146103 0.40587217 s3 0.08794364 0.009387333 -0.9938034 -0.1488195 -0.02128481 0.10155536 s4 -0.08301046 -0.932799592 -0.3457458 0.7201812 0.44631431 -0.43344591 s5 0.05988336 -0.050182504 -0.4491508 0.3967864 -0.39119033 0.01869909 > > # 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.031660843 B 0.131092396 C -0.373792256 D 0.241736035 E 0.004701737 F 1.075421369 G 0.891840248 H 0.585692361 I -0.470515635 J 0.515412302 K -0.542798940 L -0.145445304 M -0.416364470 N 0.052764840 O -0.106620325 P 0.051215795 Q 0.074539344 R -0.176101933 S -0.462341139 T 0.287133903 > > 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.1600422 -0.1572663 -0.1571479 0.2197457 0.1931256 0.09727354 > > 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.03459331 0.36525377 -0.42564299 B -0.23149676 -0.06162550 0.68639945 C 0.03186280 -1.46634124 0.31310167 D 0.05787830 -0.22684184 0.89417164 E 0.23042593 -0.07434419 -0.14197653 F 1.63935258 0.91194450 0.67496703 G -0.07731724 1.55916463 1.19367335 H 0.22920340 1.25987114 0.26800255 I 0.15566711 -1.43326829 -0.13394573 J 0.81647651 0.54862062 0.18113977 K -0.51578433 -1.20925722 0.09664473 L -0.26169734 -0.19134091 0.01670234 M -0.53930121 -0.50266463 -0.20712758 N -0.01629726 0.94768413 -0.77309236 O -0.62220293 -0.30771280 0.61005475 P -0.85381872 0.61156412 0.39590198 Q -0.48697882 0.80876879 -0.09817194 R -0.49147429 -0.11779593 0.08096442 S 0.09762849 -0.51861184 -0.96604007 T 0.90022634 -0.27708973 0.23826510 > Z2 <- colAvgsPerRowSet(X, W = W, S = S, FUN = colWeightedMeans) > print(Z2) a b c d e f s1 0.4574583 -0.7235483 -0.2537555 0.5573838 -0.04423003 0.3049865 s2 0.1991257 -0.1093846 -0.4602849 -1.2328982 0.72488580 0.4823875 s3 0.8799168 0.9898617 1.1739251 -0.3363248 0.23748200 0.2955085 > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # 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.10 0.06 0.15