R Under development (unstable) (2023-12-09 r85665 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.09463124 0.91155023 -1.85623850 0.41989710 -0.975984255 -1.77315492 B -1.01796717 -0.10563890 0.15166910 -2.22538455 1.287206164 0.96334506 C 0.64448716 2.05997331 -0.43100469 -0.04164803 0.643666883 -1.33526085 D -1.57060373 -0.67730892 0.39954586 -0.99683947 -1.211166572 -1.03659833 E -0.65541854 0.18770452 -0.98947257 -2.41722589 -0.775988816 -1.44124187 F 0.72497770 -0.64703358 0.10341937 0.80060422 0.467557671 -1.23651962 G 0.51630065 -0.71816457 -0.05127007 -0.75459300 -1.313210167 1.76291250 H 0.52921511 0.88511231 -0.42879348 0.96066706 1.267405471 -0.14771061 I -0.52104317 -0.53935357 0.23188716 -1.86820990 0.961563222 0.37860762 J 0.28213246 -0.33800931 0.24932453 0.70380841 -0.703563431 1.15880833 K 0.80525278 -2.05672869 -0.16884072 0.35332016 -1.350391721 0.05462741 L -1.57662521 -0.01403786 0.44523643 2.15345477 0.026679761 -0.03966079 M -0.68521097 0.06104978 -0.25897621 0.04375541 -1.034711647 -0.87556042 N -0.81207262 0.94652286 -0.30776070 1.55273550 0.004279816 1.05169767 O -0.16111637 -0.24568127 0.97786123 1.55468242 0.493890239 0.78786320 P 1.16450038 -0.51321705 2.35677235 -0.96561624 0.952060181 0.70266383 Q 0.57458614 0.24294938 0.49279282 1.05018034 0.276765424 0.78192737 R -0.22639895 0.42656645 -1.02714825 -0.68947763 0.072761258 1.29204713 S -0.31325761 -1.05376902 1.12735638 0.14733620 -1.565581691 0.52382013 T 0.81656649 -1.22503139 1.15155464 -0.36643892 0.146675339 1.31885616 > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # 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.40845950 -0.71817070 -1.374569590 B -0.56180303 -1.03685772 1.125275610 C 1.35223024 -0.23632636 -0.345796984 D -1.12395632 -0.29864680 -1.123882450 E -0.23385701 -1.70334923 -1.108615341 F 0.03897206 0.45201179 -0.384480975 G -0.10093196 -0.40293153 0.224851168 H 0.70716371 0.26593679 0.559847430 I -0.53019837 -0.81816137 0.670085420 J -0.02793842 0.47656647 0.227622450 K -0.62573795 0.09223972 -0.647882157 L -0.79533153 1.29934560 -0.006490517 M -0.31208059 -0.10761040 -0.955136035 N 0.06722512 0.62248740 0.527988742 O -0.20339882 1.26627183 0.640876719 P 0.32564167 0.69557806 0.827362005 Q 0.40876776 0.77148658 0.529346398 R 0.10008375 -0.85831294 0.682404192 S -0.68351332 0.63734629 -0.520880781 T -0.20423245 0.39255786 0.732765750 > > # 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.5096787 0.54714393 -0.4340071 -0.7109937 -0.06406945 -0.7954173 s2 0.2787687 -0.07309533 -0.3415292 -0.3526369 -0.08855896 -0.2656399 s3 -0.2525708 -0.73703236 0.1894018 0.3355934 -0.26642804 0.3880956 s4 -0.1234749 0.06216858 0.6919742 0.5463893 0.10387965 0.4166661 s5 0.2128740 -0.40232115 0.4361389 0.0354000 -0.26734492 0.9791627 > > # 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.56142693 B -0.15779505 C 0.25670230 D -0.84882852 E -1.01527386 F 0.03550096 G -0.09300411 H 0.51098264 I -0.22609144 J 0.22541683 K -0.39379346 L 0.16584118 M -0.45827567 N 0.40590042 O 0.56791657 P 0.61619391 Q 0.56986691 R -0.02527500 S -0.18901594 T 0.30703039 > > 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.07881633 -0.1206273 0.1083957 -0.0292496 -0.1165043 0.1445734 > > 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.40845950 -0.71817070 -1.374569590 B -0.56180303 -1.03685772 1.125275610 C 1.35223024 -0.23632636 -0.345796984 D -1.12395632 -0.29864680 -1.123882450 E -0.23385701 -1.70334923 -1.108615341 F 0.03897206 0.45201179 -0.384480975 G -0.10093196 -0.40293153 0.224851168 H 0.70716371 0.26593679 0.559847430 I -0.53019837 -0.81816137 0.670085420 J -0.02793842 0.47656647 0.227622450 K -0.62573795 0.09223972 -0.647882157 L -0.79533153 1.29934560 -0.006490517 M -0.31208059 -0.10761040 -0.955136035 N 0.06722512 0.62248740 0.527988742 O -0.20339882 1.26627183 0.640876719 P 0.32564167 0.69557806 0.827362005 Q 0.40876776 0.77148658 0.529346398 R 0.10008375 -0.85831294 0.682404192 S -0.68351332 0.63734629 -0.520880781 T -0.20423245 0.39255786 0.732765750 > Z2 <- colAvgsPerRowSet(X, W = W, S = S, FUN = colWeightedMeans) > print(Z2) a b c d e f s1 -0.55629920 0.4029557 -0.85228470 -0.9027437 0.1556110 -0.4049049 s2 -0.46305828 0.6913322 -0.01572941 -0.5192437 -0.2837498 -1.1859296 s3 0.03477958 -0.2296645 -0.44302660 -0.8083108 -0.1542156 -1.3388807 > > # - - - - - - - - - - - - - - - - - - - - - - - - - - > # 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.17