On 29.Jul.2008, at 14:13, ONKELINX, Thierry wrote:

Dear Frederike,

#Both your functions are vectorized. So you don't need loops. Working
with vectorized functions is much faster than looping.

fn <- function (x,y) {
   ifelse(x>46 & x<52 & y<12, 1, 0)
}
datagrid <- expand.grid(i = 40:60, j = 0:20)
res <- apply(datagrid, 1, function(z){
   fn(z["i"], z["j"])
})


or

outer(40:60,0:20,fn2)

which also keeps the matrix structure ...

(use as.vector(...) or as.vector(t(...)) if you need a vector)

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to