Home > Software design >  Wanting to remove vector b from matrix A by doing A[-b,] but get an error when b is null
Wanting to remove vector b from matrix A by doing A[-b,] but get an error when b is null

Time:06-17

I have a matrix A which I want to remove all rows in vector b from by doing A[-b, ].

Sometimes vector b can be NULL but when I run my code and it is NULL I get this error:

Error in -NULL : invalid argument to unary operator

How can I make sure I don't get this error in this case?

CodePudding user response:

Here is a trick:

A[-c(b, nrow(A)   1), ]

I presented this idea in a similar problem back in 2018: How to safely drop nothing from a vector when the negative index could be integer(0)? In this way, we won't worry about b being 0, integer(0) or NULL, and can use minus index safely.

  • Related