Skip to content
Prev 170097 / 398506 Next

Filter a big matrix

On Wed, Feb 11, 2009 at 6:32 AM, cruz <cruadam at gmail.com> wrote:
Wow.  It's hard to know where to begin to comment on this.

Generally we recommend taking the "whole object" approach when
possible.  For example

Xl <- X < 5

performs all the comparisons in one go, returning a logical matrix of
the same dimension as X.  If you want only those rows of X in which
every element is less than 5 you could take the matrix X < 5 and
"apply" the "&" operator across the rows.  There is a complication
here in that "&" is a binary operator, not a summary function but for
logical values the "prod" summary function has the same effect as
reduction by "&" as long as you convert the result back to a logical
value.  That is

X[as.logical(apply(Xl, 1, prod)),]

However, even before considering that aspect of the calculation it
would be best to back up and consider how you would store the result
and what you would do with it once you got it.  I really would
recommend that you think about how you are approaching the larger
problem of which, I assume, this represents one step.  You are trying
to do something difficult and the code you have outlined indicates
that you have not yet achieved fluency in R.  If indeed this approach
is the best approach to the problem then you should spend some time
reading up on R programming (Robert Gentleman's book "R Programming
for Bioinformatics" would be a good starting point I think) to save
yourself a lot of grief.

For example, paste("foo") is simply "foo".  The "$" operator extracts
a component by name but the name must be a symbol, not the value of a
variable.  If you want a named component where the name is the value
of a variable you must use x[[nm]].

When you find yourself trying to describe an algorithm as a set of
nested loops where the number of loops is variable you need to rethink
the algorithm.