用他们的rowname取消选择一组行

时间:2013-04-29 19:09:15

标签: r

我想通过他们的rowname取消选择一组行

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

A[-"RA",]

给出错误

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

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

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