どうも、ちょげ(@chogetarou)です。
colMedians()を使って行列(matrix)の列ごとの中央値を取得する方法を紹介します。
方法

colMedians()を使って行列(matrix)の列ごとの中央値を取得するには、引数を使います。
まず、colMedians()を呼び出します。
そして、colMedians()の引数に行列(matrix)を指定します。
#mtx=対象の行列
result <- colMedians(mtx)
上記のcolMedians()は、行列(matrix)の列ごとの中央値を取得します。
使用例
library(matrixStats)
nums <- matrix(c(99, -2, 91, 86, 88, 95,
33, 28, 31, 0, 39, 34,
30, 28, 24, 24, -5, 28,
1, 4, 11, 0, 2, -10), nrow=4)
result <- colMedians(nums, na.rm=TRUE)
nums
cat("中央値:", result)
出力:
> nums
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 99 88 31 30 -5 11
[2,] -2 95 0 28 28 0
[3,] 91 33 39 24 1 2
[4,] 86 28 34 24 4 -10
> cat("中央値:", result)
中央値: 88.5 60.5 32.5 26 2.5 1
コメント