为什么重新排序将单列数据帧转换为向量?

时间:2014-06-27 19:56:43

标签: r

我很好奇为什么重新排序单个圆柱数据框(或矩阵)会将其转换为矢量。这有什么理由吗?

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"

1 个答案:

答案 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]