1

我想按行名取消选择一组行

A <- data.frame(c("a","b","c"),c("d","e","f"))
rownames(A) <- c("RA","RB","RC")

A[-"RA",]

给出错误

4

2 回答 2

5

试试setdiff

您的数据(具有更好的列名):

> A <- data.frame(V1 = c("a","b","c"), V2 = c("d","e","f"))
> rownames(A) <- c("RA","RB","RC")
> A
   V1 V2
RA  a  d
RB  b  e
RC  c  f

您尝试了什么,以及由此产生的错误:

> A[-"RA",]
Error in -"RA" : invalid argument to unary operator

利用setdiff

> A[setdiff(rownames(A), "RA"), ]
   V1 V2
RB  b  e
RC  c  f
于 2013-04-29T19:12:09.410 回答
2

data.table提供与您正在寻找的非常接近的语法。与其拥有行名,不如创建一个列(我在下面的示例中将其称为“信息”)并将该列设置为键。然后您可以执行以下操作:

library(data.table)
A = data.table(c("a","b","c"), c("d","e","f"), info=c("RA","RB","RC"),
               key = "info")
#   V1 V2 info
#1:  a  d   RA
#2:  b  e   RB
#3:  c  f   RC

A["RA"]
#   info V1 V2
#1:   RA  a  d

A[!"RA"]
#   V1 V2 info
#1:  b  e   RB
#2:  c  f   RC
于 2013-04-29T20:08:22.667 回答