Reputation: 1036
Here is my problem:
gg <- matrix(0,4,4)
class(gg)
class(gg[,1])
R is being a little too helpful here; I'd prefer that the one-column matrix stayed a matrix. This works, but it gets expensive:
class(t(t(gg[,1])))
And this works, but it seems like it shouldn't have to be necessary:
class(matrix(gg[,1], ncol=1))
Can you recommend a nice alternative, as brainless, effortless, and costless as possible?
Upvotes: 1
Views: 101
Reputation: 193527
Use the argument drop = FALSE
:
gg[, 1, drop = FALSE]
# [,1]
# [1,] 0
# [2,] 0
# [3,] 0
# [4,] 0
class(gg[,1, drop = FALSE])
# [1] "matrix"
Upvotes: 4