Skip to content
Prev 180529 / 398506 Next

memory usage grows too fast

Hi William,

Thanks for the comments and explanation.
It is really good to know the details of rowMeans.
I did modified Peter's codes from length(x[x=="02"]) to sum(x=="02"), though it improved only in few seconds. :)

Best,
Mike

-----Original Message-----
From: William Dunlap [mailto:wdunlap at tibco.com] 
Sent: Friday, May 15, 2009 10:09 AM
To: Ping-Hsun Hsieh
Subject: RE: [R] memory usage grows too fast

rowMeans(dataMatrix=="02") must
  (a) make a logical matrix the dimensions of dataMatrix in which to put
       the result of dataMatrix=="02" (4 bytes/logical element)
  (b) make a double precision matrix (8 bytes/element) the size of that
       logical matrix because rowMeans uses some C code that only works
on
       doubles
apply(dataMatrix,1,function(x)length(x[x=="02"])/ncol(dataMatrix))
never has to make any copies of the entire matrix.  It extracts a row
at a time and when it is done with the row, the memory used for
working on the row is available for other uses.  Note that it would
probably
be a tad faster if it were changed to
   apply(dataMatrix,1,function(x)sum(x=="02")) / ncol(dataMatrix)
as sum(logicalVector) is the same as length(x[logicalVector]) and there
is no need to compute ncol(dataMatrix) more than once.

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com
Message-ID: <0D57C92D2BD3C24ABA05093F55D3EC05A800BB@EX-BE07.ohsu.edu>
In-Reply-To: <77EB52C6DD32BA4D87471DCD70C8D70001335A3E@NA-PA-VBE03.na.tibco.com>