の要素をmatrix
に置き換え、simplify2array
を使用してrowMeans
を計算します。回避策はrowMeans(matrix(as.numeric(bar_new), ncol = ncol(bar_new)))
だろう。もちろん、簡易行列を作成して数値行列を作成する
bar <- list(list(11, 21, 31), list(12, 22, 32))
bar_new <- simplify2array(bar)
#gives[,1] [,2]
#[1,] 11 12
#[2,] 21 22
#[3,] 31 32
rowMeans(bar_new)
#gives Error: 'x' must be numeric
# does not work as all elements in foo are lists - see class(bar_new[1, 1])
:問題はsimplify2array
がクラスlist
のbeeingて要素と、matrix
を生成しないnumeric
ということです。しかし、私はそれを短くてシンプルにしたい。だから問題は:simplify2array
を "数値"マトリックスを生成するにはどうすればいいですか?
完全を期すため、予想される出力は次のようになります。
foo <- matrix(c(11, 12, 21, 22, 31, 32), byrow = TRUE, nrow = 3)
#gives [,1] [,2]
#[1,] 11 12
#[2,] 21 22
#[3,] 31 32
rowMeans(foo)
#gives 11.5 21.5 31.5
# works as all elements in foo are numeric - see class(foo[1, 1])
をそれだけではなく、答えとして、私はこの答えを受け入れ私の質問ですが、私はまた、オブジェクトのモードをどのように使用し、変更するかを学びました。 – Qaswed
@Qaswed、 'mode'の代わりに' storage.mode'を使うかもしれないことに注意してください。おそらくもっと直感的です。 – Julius