在 data.table 对象中显示行名 [英] Display row names in a data.table object
问题描述
参考:在尝试回答时 这个非常基本的问题,我突然意识到我无法在 data.table
对象中显示 rownames
Reference: While trying to answer this very basic question, I suddenly realized that I wasn't able to display rownames
in a data.table
object
玩具示例
library(data.table)
DT <- data.table(A = letters[1:3])
DT
## A
## 1: a
## 2: b
## 3: c
row.names(DT) <- 4:6
row.names(DT)
## [1] "4" "5" "6" # seem to work
或
rownames(DT) <- 7:9
rownames(DT)
## [1] "7" "8" "9" # seems to be ok too
但是当显示数据本身时,行名保持不变
But when displaying the data itself, row names remains unchanged
DT
## A
## 1: a
## 2: b
## 3: c
我会假设 data.table
出于效率目的而忽略不必要的属性,但 attributes
似乎不同意
I would assume data.table
ignores unnecessary attributes for efficiency purposes, but attributes
seem to disagree
attributes(DT)
# $names
# [1] "A"
#
# $row.names
# [1] 7 8 9
#
# $class
# [1] "data.table" "data.frame"
#
# $.internal.selfref
# <pointer: 0x0000000000200788>
推荐答案
这或多或少是逐字逐句的.
This is more or less verbatim from comments.
data.table
不支持行名.这是故意的,因为行名是一个糟糕的设计选择,因为它们比列使用起来麻烦得多(尤其是在 data.table
中,列比在data.frame
)并且只是可以表示的数据列类型的一个子集(回想一下 data.frame
中的行名只是一个字符向量,而列可以是任何东西).
data.table
doesn't support row names. This is intentional, as row names are a bad design choice, because they are far more cumbersome to use than columns (and especially so in data.table
, where columns are so much easier to deal with than in data.frame
) and are only a subset of what kind of data columns can represent (recall that row names in data.frame
are a character vector only, whereas columns can be anything).
这篇关于在 data.table 对象中显示行名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!