按行名称索引 data.frame 时出现意外行为
Unexpected behaviour in indexing data.frame by row name
我不经常使用行名索引 data.frame,但有时这样做有好处。但是,当我尝试过滤不令人兴奋的行时,我注意到了意想不到的结果
test <- data.frame(a = c("a", "b", "c"),
b = c("A", "B", "C"),
row.names = c(-99.5, 99.5, 99))
test["-99", ]
你会期望它会给你
a b
NA <NA> <NA>
但是 returns
a b
-99.5 a A
具体一点
Session info ---------------------------------------------------------------
setting value
version R version 3.2.1 (2015-06-18)
system x86_64, mingw32
ui RStudio (0.99.441)
language (EN)
collate English_United Kingdom.1252
tz Europe/London
有什么想法吗?
这确实出乎意料
答案在于索引时行名的部分匹配:
mtcars["Val", ]
将为我们提供 "Valient" 行。这不适用于列:
mtcars[ ,"cy"]
为了消除这个问题,我将使用以下子集:
subset(test, rownames(test) == "-99")
编辑:确实记录在 ?"[.data.frame"
Both [ and [[ extraction methods partially match row names. By default
neither partially match column names, but [[ will if exact = FALSE
(and with a warning if exact = NA). If you want to exact matching on
row names use match, as in the examples.
要对您的数据使用匹配:
test[match("-99", row.names(test)), ]
我不经常使用行名索引 data.frame,但有时这样做有好处。但是,当我尝试过滤不令人兴奋的行时,我注意到了意想不到的结果
test <- data.frame(a = c("a", "b", "c"),
b = c("A", "B", "C"),
row.names = c(-99.5, 99.5, 99))
test["-99", ]
你会期望它会给你
a b
NA <NA> <NA>
但是 returns
a b
-99.5 a A
具体一点
Session info ---------------------------------------------------------------
setting value
version R version 3.2.1 (2015-06-18)
system x86_64, mingw32
ui RStudio (0.99.441)
language (EN)
collate English_United Kingdom.1252
tz Europe/London
有什么想法吗?
这确实出乎意料
答案在于索引时行名的部分匹配:
mtcars["Val", ]
将为我们提供 "Valient" 行。这不适用于列:
mtcars[ ,"cy"]
为了消除这个问题,我将使用以下子集:
subset(test, rownames(test) == "-99")
编辑:确实记录在 ?"[.data.frame"
Both [ and [[ extraction methods partially match row names. By default neither partially match column names, but [[ will if exact = FALSE (and with a warning if exact = NA). If you want to exact matching on row names use match, as in the examples.
要对您的数据使用匹配:
test[match("-99", row.names(test)), ]