R Under development (unstable) (2024-09-06 r87103 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.20037583 -5.104421e-01 -0.09107497 0.67575190 1.72591527 0.43679037 B -0.03735777 2.248526e+00 0.42895357 -0.06425426 -0.67461510 -2.42820636 C 0.76892791 1.613916e+00 -0.16164531 -0.45512507 -0.90029971 -0.73757658 D -0.26452470 1.269226e+00 -1.30370327 0.67481158 -0.07365578 -1.33523445 E 0.67522799 9.756553e-01 1.33292065 0.71828896 -1.07243519 -1.30087109 F 0.60774637 1.533094e+00 0.94131654 -0.08842390 1.20435053 -0.86640656 G 0.35138101 -3.091968e-01 0.13228230 -1.26353002 -0.83660496 0.68641877 H -0.23341156 5.698523e-01 -1.72890742 -0.58281366 -0.30354309 0.46087358 I 0.42639450 -1.276546e+00 -0.68580416 0.78071749 0.35390748 1.24078036 J -0.84814287 -2.920733e-01 -3.21860860 1.60496870 -0.20325067 1.00950871 K -0.72012306 -2.058729e+00 0.38212090 -3.22719981 -0.85016832 -0.07315693 L -1.11671073 -9.367211e-05 2.61497724 0.39022831 1.54073648 -0.06752564 M -0.20827342 6.194401e-02 0.59312348 -0.18011050 0.92048031 -0.19693121 N 1.25805912 1.746167e+00 0.88420545 0.12256304 -0.32455078 -0.68666491 O -2.71087486 6.104583e-02 -1.08333972 -0.31666455 -0.56411209 1.21461178 P -1.24501177 1.055406e+00 0.29638587 0.58509555 0.85727518 -0.60305159 Q 1.09843071 -1.381543e+00 0.35748704 -2.18374900 0.51374183 -1.20577559 R 0.58086888 -3.611942e-01 -0.81526452 -0.52274048 0.36895692 -0.07514869 S 0.03485553 6.782447e-01 0.49088805 1.02163208 0.67692515 -0.46137796 T 0.69368040 -8.193664e-01 -0.63363189 -1.13698737 1.93821858 0.93707701 > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # 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.15503315 0.29233846 1.08135282 B 1.10558428 0.18234965 -1.55141073 C 1.19142179 -0.30838519 -0.81893814 D 0.50235044 -0.31444585 -0.70444512 E 0.82544165 1.02560481 -1.18665314 F 1.07042012 0.42644632 0.16897199 G 0.02109209 -0.56562386 -0.07509309 H 0.16822038 -1.15586054 0.07866525 I -0.42507553 0.04745667 0.79734392 J -0.57010807 -0.80681995 0.40312902 K -1.38942626 -1.42253945 -0.46166263 L -0.55840220 1.50260277 0.73660542 M -0.07316470 0.20650649 0.36177455 N 1.50211284 0.50338424 -0.50560785 O -1.32491452 -0.70000214 0.32524984 P -0.09480274 0.44074071 0.12711180 Q -0.14155616 -0.91313098 -0.34601688 R 0.10983736 -0.66900250 0.14690411 S 0.35655010 0.75626006 0.10777359 T -0.06284299 -0.88530963 1.43764780 > > # 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.1668553 1.1553064 -0.2818675 0.20779604 0.01933617 -1.01605676 s2 0.3502360 0.6923512 0.1694030 -0.30411965 -0.25205818 -0.25499632 s3 -0.5646455 -0.9068605 -0.2268287 -0.11282133 0.21030624 0.52740162 s4 -0.7265252 0.7311407 0.1725938 0.05272088 0.22227315 -0.06800898 s5 0.6019589 -0.4709647 -0.1501303 -0.70546119 0.87446062 -0.20130631 > > # 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.40621938 B -0.08782560 C 0.02136615 D -0.17218018 E 0.22146444 F 0.55527948 G -0.20654162 H -0.30299164 I 0.13990835 J -0.32459967 K -1.09120945 L 0.56026866 M 0.16503878 N 0.49996308 O -0.56655560 P 0.15768325 Q -0.46690134 R -0.13742034 S 0.40686125 T 0.16316506 > > 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.03442412 0.2401946 -0.06336594 -0.1723771 0.2148636 -0.2025933 > > 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.15503315 0.29233846 1.08135282 B 1.10558428 0.18234965 -1.55141073 C 1.19142179 -0.30838519 -0.81893814 D 0.50235044 -0.31444585 -0.70444512 E 0.82544165 1.02560481 -1.18665314 F 1.07042012 0.42644632 0.16897199 G 0.02109209 -0.56562386 -0.07509309 H 0.16822038 -1.15586054 0.07866525 I -0.42507553 0.04745667 0.79734392 J -0.57010807 -0.80681995 0.40312902 K -1.38942626 -1.42253945 -0.46166263 L -0.55840220 1.50260277 0.73660542 M -0.07316470 0.20650649 0.36177455 N 1.50211284 0.50338424 -0.50560785 O -1.32491452 -0.70000214 0.32524984 P -0.09480274 0.44074071 0.12711180 Q -0.14155616 -0.91313098 -0.34601688 R 0.10983736 -0.66900250 0.14690411 S 0.35655010 0.75626006 0.10777359 T -0.06284299 -0.88530963 1.43764780 > Z2 <- colAvgsPerRowSet(X, W = W, S = S, FUN = colWeightedMeans) > print(Z2) a b c d e f s1 0.08150903 0.8690421 0.1689393 0.3057488 0.52565008 -0.995708 s2 0.25220160 1.4415706 -0.7326743 0.1098433 -0.48697775 -1.036406 s3 0.64148718 1.2543746 1.1371186 0.3149325 0.06595767 -1.083639 > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # 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.20 0.06 0.61