apply function using expand.grid in R

Posted by kolonel on Stack Overflow See other posts from Stack Overflow or by kolonel
Published on 2014-08-24T21:34:13Z Indexed on 2014/08/24 22:20 UTC
Read the original article Hit count: 148

Filed under:
|

I have two vectors x and y. I create a grid using the following function:

v = expand.grid(x, y)

I have a function defined as follows

N <- function(a, b , dat){
     m = ncol(Filter(function(z) a*max(z)*min(z) < b , dat[1:ncol(dat)]))
     return(m)
}

and then I need to maximize N over a grid of x,y:

Maximize <- function(x , y ,dat){
          v = as.matrix(expand.grid(x,y))
          # Here is where I want to map the values of v and get the maximum element and 
          # get the tuple in v that maximized N
          temp1 <- max(apply(v , 1 , N(v[[1]] , v[[2]] , dat)))
}

Thanks

© Stack Overflow or respective owner

Related posts about r

    Related posts about map