需要减少矩阵,直到它只剩下一个元素。这是正常工作,直到我有一个2x2矩阵。当我从2x2矩阵中删除一行或一列时,而不是获得1x2或2x1矩阵,我最终得到一个向量,我的代码就会爆炸。
测试用例:
# 2x2 matrix. remove row and column to get 1x1 matrix
testm <- matrix( data=0, nrow=2, ncol=2, dimnames=list(c("A","B"),c("A","B")))
print(testm)
print(is.matrix(testm)) # TRUE
testm <- testm[-1,] # should result in 1x2 matrix
print(testm) # vector
print(is.matrix(testm)) # FALSE
testm <- testm[,-1] # error
print(testm)
&#13;
答案 0 :(得分:1)
尝试
testm <- testm[-1,,drop=FALSE]