这是我的问题:
gg <- matrix(0,4,4)
class(gg)
class(gg[,1])
R在这里有点太有用了;我希望单列矩阵保持矩阵。这有效,但它变得昂贵:
class(t(t(gg[,1])))
这很有效,但似乎不应该有必要:
class(matrix(gg[,1], ncol=1))
你能推荐一个不错的选择,尽可能无脑,轻松,无成本吗?
答案 0 :(得分:3)
使用参数drop = FALSE
:
gg[, 1, drop = FALSE]
# [,1]
# [1,] 0
# [2,] 0
# [3,] 0
# [4,] 0
class(gg[,1, drop = FALSE])
# [1] "matrix"