[R] out of memory?

Luke Tierney luke at stat.umn.edu
Thu Mar 22 13:19:26 CET 2001


news_vkhamenya at chat.ru wrote:
> Hello r-help,
> 
> why call:
> 
> #----------
> outer(1:1000, 1:2, function(r,c) ifelse(m[r,c]<=0,.0001,m[r,c]) )
> #----------
> 
> for matrix m with only 1000 rows and 2 columns forces my PC to use
> more than 250Mb(!) of virtual memory?  strange...
> 

I think the source of the confusion here is in how outer uses its FUN
argument, and hence how that function has to be written.  Here is an
example that should show what is happening:

> f<-function(x,y) {
+ cat("x = ", x, "\n")
+ cat("y = ", y, "\n")
+ x+y
+ }
> outer(1:3,1:2,f)
x =  1 2 3 1 2 3 
y =  1 1 1 2 2 2 
     [,1] [,2]
[1,]    2    3
[2,]    3    4
[3,]    4    5
>

[instead of printing, you can also use debug(f) to see what is going on]

There is *one* call to FUN with arguments that are the appropriate
replications of X and Y, not 3*2=6 calls to FUN with scalar arguments.
This is done since one call to a vectorized function will be much
faster than many calls to functions working with scalar values.  The
help for outer hints at this but could be a bit more explicit on this
since it is a natural point of confusion.

As a result, as has been pointed out by others, outer isn't the right tool
for this problem.

Hope that helps.

luke
-- 
Luke Tierney
University of Minnesota                      Phone:           612-625-7843
School of Statistics                         Fax:             612-624-8868
313 Ford Hall, 224 Church St. S.E.           email:      luke at stat.umn.edu
Minneapolis, MN 55455 USA                    WWW:  http://www.stat.umn.edu
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list