2

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

7
  • Couldn't you just do v$x + v$y? (It may be helpful to think of v not as a collection of two-dimensional tuples but as two parallel vectors) Commented Aug 24, 2014 at 21:35
  • Hi @DavidRobinson, I plan to use this concept for a more complicated function inside map. Commented Aug 24, 2014 at 21:36
  • I am doing some optimization over a grid, so I need to pass a function that takes elements from the cartesian product of the two elements. Commented Aug 24, 2014 at 21:38
  • so I want to pass the entire values from the grid to a function, where function is applied iteratively to each of the elements in the list, which then I can take the minimum of. Commented Aug 24, 2014 at 21:39
  • You'll probably want to provide more context in the original question. Commented Aug 24, 2014 at 21:39

2 Answers 2

5

Map will not iterate over rows of your list like that. How about

x <- 1:3
y <- 11:13
v <- expand.grid(x, y)
do.call(mapply, c(function(a, b) a+b, unname(v)))
# [1] 12 13 14 13 14 15 14 15 16

Here each value is passed as a separate parameter to the function.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use apply for this:

apply(v, 1, myfunction)

Where myfunction is your function (you've clarified in the comments that it is more complicated than just addition). 1 means that you want to apply over the rows (2 would mean to apply to the columns).

If x and y are both numeric (or both character vectors)*, you certainly want to turn v into a matrix first:

apply(as.matrix(v), 1, myfunction)

This will be more efficient and make the value passed to myfunction a vector rather than a one-row data frame.

However, if the contents of myfunction are some kind of numeric operation, there's a good chance you could vectorize it instead.


*If x and y are both character vectors, don't forget to use stringsAsFactors=FALSE in your expand.grid call.

12 Comments

Thank you. How can I use the elements of the row as arguments? should do function(x) x[1] + x[2] ?
A better way to ask I guess is how can I access the element of the rows in the function ?
@kolonel: Yes, if you followed my advice to use as.matrix first. If you applied it directly to the data frame, it would instead be x[[1]] + x[[2]].
Thank you. It seems a bit counter-intuitive the way the elements are accessed does it not?
@kolonel I'm not sure I'd say it's counter intuitive. Again, I'd think a more intuitive (and possibly much more efficient) approach would be to vectorize your operation. If you shared the details of the function I could help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.