1library("matrixStats")
2
3X <- matrix(rnorm(20 * 6), nrow = 20, ncol = 6)
4rownames(X) <- LETTERS[1:nrow(X)]
5colnames(X) <- letters[1:ncol(X)]
6print(X)
7
8
9# - - - - - - - - - - - - - - - - - - - - - - - - - -
10# Apply rowMeans() for 3 sets of 2 columns
11# - - - - - - - - - - - - - - - - - - - - - - - - - -
12nbr_of_sets <- 3L
13S <- matrix(1:ncol(X), ncol = nbr_of_sets)
14colnames(S) <- sprintf("s%d", 1:nbr_of_sets)
15print(S)
16
17Z <- rowAvgsPerColSet(X, S = S)
18print(Z)
19
20# Validation
21Z0 <- cbind(s1 = rowMeans(X[, 1:2]), s2 = rowMeans(X[, 3:4]),
22            s3 = rowMeans(X[, 5:6]))
23stopifnot(identical(drop(Z), Z0))
24
25
26# - - - - - - - - - - - - - - - - - - - - - - - - - -
27# Apply colMeans() for 5 sets of 4 rows
28# - - - - - - - - - - - - - - - - - - - - - - - - - -
29nbr_of_sets <- 5L
30S <- matrix(1:nrow(X), ncol = nbr_of_sets)
31colnames(S) <- sprintf("s%d", 1:nbr_of_sets)
32print(S)
33
34Z <- colAvgsPerRowSet(X, S = S)
35print(Z)
36
37# Validation
38Z0 <- rbind(s1 = colMeans(X[1:4, ]), s2 = colMeans(X[5:8, ]),
39            s3 = colMeans(X[9:12, ]), s4 = colMeans(X[13:16, ]),
40            s5 = colMeans(X[17:20, ]))
41stopifnot(identical(drop(Z), Z0))
42
43
44# - - - - - - - - - - - - - - - - - - - - - - - - - -
45# When there is only one "complete" set
46# - - - - - - - - - - - - - - - - - - - - - - - - - -
47nbr_of_sets <- 1L
48S <- matrix(1:ncol(X), ncol = nbr_of_sets)
49colnames(S) <- sprintf("s%d", 1:nbr_of_sets)
50print(S)
51
52Z <- rowAvgsPerColSet(X, S = S, FUN = rowMeans)
53print(Z)
54
55Z0 <- rowMeans(X)
56stopifnot(identical(drop(Z), Z0))
57
58
59nbr_of_sets <- 1L
60S <- matrix(1:nrow(X), ncol = nbr_of_sets)
61colnames(S) <- sprintf("s%d", 1:nbr_of_sets)
62print(S)
63
64Z <- colAvgsPerRowSet(X, S = S, FUN = colMeans)
65print(Z)
66
67Z0 <- colMeans(X)
68stopifnot(identical(drop(Z), Z0))
69
70
71
72# - - - - - - - - - - - - - - - - - - - - - - - - - -
73# Use weights
74# - - - - - - - - - - - - - - - - - - - - - - - - - -
75nbr_of_sets <- 3L
76S <- matrix(1:ncol(X), ncol = nbr_of_sets)
77colnames(S) <- sprintf("s%d", 1:nbr_of_sets)
78print(S)
79
80W <- matrix(runif(length(X)), nrow = nrow(X), ncol = ncol(X))
81Z1 <- rowAvgsPerColSet(X, W = W, S = S, FUN = rowWeightedMeans)
82print(Z1)
83Z2 <- colAvgsPerRowSet(X, W = W, S = S, FUN = colWeightedMeans)
84print(Z2)
85
86# - - - - - - - - - - - - - - - - - - - - - - - - - -
87# Result should always be a matrix, including when nrow(X) <= 1
88# (https://github.com/HenrikBengtsson/matrixStats/issues/108)
89# - - - - - - - - - - - - - - - - - - - - - - - - - -
90X <- matrix(1:3, nrow = 1L, ncol = 3L)
91S <- matrix(1, nrow = 1L, ncol = 1L)
92Z1 <- rowAvgsPerColSet(X, S = S)
93stopifnot(is.matrix(Z1))
94Z2 <- rowAvgsPerColSet(X, S = S, rows = 0)
95stopifnot(is.matrix(Z2))
96
97
98# - - - - - - - - - - - - - - - - - - - - - - - - - -
99# Works with many, one or zero columns / rows
100# (https://github.com/HenrikBengtsson/matrixStats/issues/172)
101# - - - - - - - - - - - - - - - - - - - - - - - - - -
102S <- cbind(1:2, 3:4, 5:6)
103X <- matrix(rnorm(2 * 6), nrow = 6, ncol = 2)
104Z2 <- colAvgsPerRowSet(X, S = S, FUN = colSums2)
105Z2_ref <- rbind(colSums2(X[S[,1], ,drop=FALSE]),
106                colSums2(X[S[,2], ,drop=FALSE]),
107                colSums2(X[S[,3], ,drop=FALSE]))
108stopifnot(identical(Z2, Z2_ref))
109X <- matrix(rnorm(6), nrow = 6, ncol = 1)
110Z1 <- colAvgsPerRowSet(X, S = S, FUN = colSums2)
111Z1_ref <- rbind(colSums2(X[S[,1], ,drop=FALSE]),
112                colSums2(X[S[,2], ,drop=FALSE]),
113                colSums2(X[S[,3], ,drop=FALSE]))
114stopifnot(identical(Z1, Z1_ref))
115X <- matrix(numeric(0), nrow = 6, ncol = 0)
116Z0 <- colAvgsPerRowSet(X, S = S, FUN = colSums2)
117Z0_ref <- matrix(numeric(0), nrow = ncol(S), ncol = 0)
118stopifnot(identical(Z0, unname(Z0_ref)))
119
120
121S <- rbind(1:4, 5:8)
122X <- matrix(rnorm(n = 2 * 8), nrow = 2, ncol = 8)
123Z2 <- rowAvgsPerColSet(X, S = S, FUN = rowMeans2)
124Z2_ref <- cbind(rowMeans2(X[,S[,1],drop=FALSE]),
125                rowMeans2(X[,S[,2],drop=FALSE]),
126                rowMeans2(X[,S[,3],drop=FALSE]),
127                rowMeans2(X[,S[,4],drop=FALSE]))
128stopifnot(identical(Z2, Z2_ref))
129X <- matrix(rnorm(n = 8), nrow = 1, ncol = 8)
130Z1 <- rowAvgsPerColSet(X, S = S, FUN = rowMeans2)
131Z1_ref <- cbind(rowMeans2(X[,S[,1],drop=FALSE]),
132                rowMeans2(X[,S[,2],drop=FALSE]),
133                rowMeans2(X[,S[,3],drop=FALSE]),
134                rowMeans2(X[,S[,4],drop=FALSE]))
135stopifnot(identical(Z1, Z1_ref))
136X <- matrix(numeric(0), nrow = 0, ncol = 8)
137Z0 <- rowAvgsPerColSet(X, S = S, FUN = rowMeans2)
138Z0_ref <- matrix(numeric(0), nrow = 0, ncol = ncol(S))
139stopifnot(identical(Z0, Z0_ref))
140