我有這樣的矩陣:一些值替換矩陣NA
mat=matrix(c(1,1,1,2,2,2,3,4,NA,
4,4,4,4,4,3,5,6,4,
3,3,5,5,6,8,0,9,NA,
1,1,1,1,1,4,5,6,1),nrow=4,byrow=TRUE)
print(mat)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
# [1,] 1 1 1 2 2 2 3 4 NA
# [2,] 4 4 4 4 4 3 5 6 4
# [3,] 3 3 5 5 6 8 0 9 NA
# [4,] 1 1 1 1 1 4 5 6 1
我應更換NA與其他值的值,以這種方式:
我還有一個矩陣:
mat2=matrix(c(24,1,3,2, 4,4,4,4, 3,2,2,5, 1,3,5,1),nrow=4,byrow=TRUE)
[,1] [,2] [,3] [,4]
[1,] 24 1 3 2
[2,] 4 4 4 4
[3,] 3 2 2 5
[4,] 1 3 5 1
以及具有第一矩陣「mat」的行的索引NA
的子集:
subset=c(1,3)
我想重新將矩陣的NA
與具有最大值的行的值的列名重新組合。
,我將有「1」的第一行,「4」爲第三個,我不關心行2和4
請參閱「?max.col」。 [here](http://stackoverflow.com/questions/8220343/is-there-something-like-a-pmax-index) - 'max.col(mat2 [subset,],「first」)' –
Your 'mat'是一個字符矩陣。對於NA值,使用不帶引號的「NA」。 – Roland