どうも、ちょげ(@chogetarou)です。
行列(matrix)の列数をカウントする方法を紹介します。
方法

行列(matrix)の列数をカウントするには、ncol()を使います。
まず、ncol()を呼び出します。
そして、ncol()の引数に行列を指定します。
#mtx=対象の行列
result <- ncol(mtx)
上記のncol()は、対象の行列(matrix)の列数をカウントし、行列の列数を返します。
使用例
nums <- matrix(1:20, nrow=5)
result <- ncol(nums)
nums
cat("列数:", result)
出力:
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
列数: 4
コメント