36

I have matrix g:

> g[1:5,1:5]
        rs7510853 rs10154488 rs12159982 rs2844887 rs2844888
NA06985 "CC"      "CC"       "CC"       "CC"      "CC"     
NA06991 "CC"      "CC"       "CC"       "CC"      "CC"     
NA06993 "CC"      "CC"       "CC"       "CC"      "CC"     
NA06994 "CC"      "CC"       "CC"       "CC"      "CC"     
NA07000 "CC"      "CC"       "CC"       "CC"      "CC"     
> rownames(g)[1:2]->remove
> remove
[1] "NA06985" "NA06991"
> g[-remove,]

Error in -remove : invalid argument to unary operator

Is there a simple way to do what I want to do here (remove the ID's referenced in the vector 'remove' from matrix g?

Note: this is just a model for what I actually want to do, please don't say just do g[-(1:2), ], I need to be able to remove a whole bunch of rows that I have ID-d.

starball
  • 20,030
  • 7
  • 43
  • 238
JoshDG
  • 3,871
  • 10
  • 51
  • 85

3 Answers3

71

When working with indexing, you cannot use "negative" character vectors. You can convert to logical with %in%

g[!rownames(g) %in% remove, ]  # ! is logical negation

If you really wanted to use negative-indexing this could be done:

g[-which(rownames(g) %in% remove), ] #which converts to numeric, so minus sign OK

... however it has a nasty potential erroneous result that arises when there are not any rownames in the target vector. The result may be no values returned.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
7

I use "setdiff" as follows:

g[setdiff(rownames(g),remove),]
Ofri Mann
  • 361
  • 3
  • 7
7

You cannot negative index a character vector when indexing. Turn your vector remove into a boolean. I've defined a function

`%notin%` <- function(x,y) !(x %in% y) 

which can then be used as such: g[rownames(g) %notin% remove ,]

Chase
  • 67,710
  • 18
  • 144
  • 161
  • 1
    I've seen people name that function `%!in%` and I think the `?'%in%'` page demonstrates building a similar function calling it `%w/o%`. – IRTFM Oct 10 '13 at 18:08