File: colMeans.R

package info (click to toggle)
r-cran-foreach 1.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 648 kB
  • sloc: makefile: 2
file content (20 lines) | stat: -rw-r--r-- 518 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# compute the mean of the columns and the rows of a matrix

library(foreach)

# generate the input matrix
x <- matrix(rnorm(100 * 100), 100)

# compute the mean of each column of x
cmeans <- foreach(i=1:ncol(x), .combine=c) %do% mean(x[,i])

# check the results
expected <- colMeans(x)
print(all.equal(cmeans, expected))

# compute the mean of each row of x
rmeans <- foreach(i=1:nrow(x), .combine=c) %do% mean(x[i,])

# check the results
expected <- rowMeans(x)
print(all.equal(rmeans, expected))