我想按行名取消选择一组行
A <- data.frame(c("a","b","c"),c("d","e","f"))
rownames(A) <- c("RA","RB","RC")
但
A[-"RA",]
给出错误
我想按行名取消选择一组行
A <- data.frame(c("a","b","c"),c("d","e","f"))
rownames(A) <- c("RA","RB","RC")
但
A[-"RA",]
给出错误
试试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
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