outer() function with max()

Hello all,

I am trying to use the function outer() together with max() to find the maximum between every pair of values in matrix form.

I will use a simple example:

x = 1:4
y = 1:4

outer(x, y, FUN="max")

However, what I get is an error.

Is there any way in which I can do this operation in R?

Thanks in advance for any help!

apply() is a good way to run a function on either every row or every column of a matrix.
expand.grid is a way of making all possible combinations between two vectors

(mymat <- expand.grid(1:4,
                      1:4) %>% as.matrix)

mynewmat <- cbind(mymat,
                  apply(mymat,
                        MARGIN = 1,
                        FUN = max))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.