我很好奇为什么重新排序单个圆柱数据框(或矩阵)会将其转换为矢量。这有什么理由吗?
k <- data.frame(a=c(2,10,3), b=c(8,3,9))
k <- k[order(k[,1]),]
class(k)
# [1] "data.frame"
k <- data.frame(a=c(2,10,3))
k <- k[order(k[,1]),]
class(k)
# [1] "numeric"
答案 0 :(得分:4)
查看?'['
特别是drop
参数
drop: For matrices and arrays. If ‘TRUE’ the result is coerced to
the lowest possible dimension (see the examples). This only
works for extracting elements, not for the replacement. See
‘drop’ for further details.
要回答您的问题,您需要
k[order(k[,1]), , drop=FALSE]