4
set.seed(8) data <- data.frame(A=rnorm(10), B=rnorm(10)) fun <- function(df,x,y){ require(dplyr) res <- filter(df,A<x,B>y) %>% nrow() return(res) } 

This work for single values of x and y:

fun(x=1,y=0,df=data) 

I would like to do use outer() (or similar) to do combinations of an x and y but cannot figure out how to pass the df argument. It seems to be the same issue as in here: Using outer() with a multivariable function. But passing df through ... does not work:

outer(x=c(0,2),y=c(0,2),fun,df=data) 

What is missing ?

4
  • The first two arguments of fun must be your x and y and it must be vectorized with regard to these arguments if you want to use outer. Commented Jul 6, 2015 at 15:51
  • @Roland: Could you elaborate...'must be vectorized' Commented Jul 6, 2015 at 15:54
  • 1
    Vectorize(yourFucntion...., vec=c("x", "y")) Commented Jul 6, 2015 at 15:54
  • @Legalizelt: That I did not get Im sorry Commented Jul 6, 2015 at 15:58

3 Answers 3

5

I'd suggest using cut:

# borrowing the @Colonel's example: x = c(0,1,2) y = c(-1,0,2) library(magrittr) data %<>% mutate( Ag = cut(A,c(-Inf,x,Inf)), Bg = cut(B,c(-Inf,y,Inf)) ) with(data, table(Ag,Bg)) # Bg # Ag (-Inf,-1] (-1,0] (0,2] (2, Inf] # (-Inf,0] 1 4 3 0 # (0,1] 0 0 2 0 # (1,2] 0 0 0 0 # (2, Inf] 0 0 0 0 

This may not match the inequalities the OP is after, but I suspect some variation would do the trick. Note that x and y have to be sorted for cut to work.

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

Comments

3

Vectorized arguments means your function can take vectors as arguments(!). As @Roland described in the comments, your function needs to be specifically setup to work with outer. So the first two args should be vectorized. This means that you can pass a vector of arguments for x and y and the function will be called on each value of the two. You can use the Vectorize function to do this easily.

fun <- Vectorize(function(x, y, df){ require(dplyr) res <- filter(df,A<x,B>y) %>% nrow() return(res) }, vectorize.args=c("x", "y")) outer(c(0,1,2), c(-1,0,2), fun, df=data) # [,1] [,2] [,3] # [1,] 7 3 0 # [2,] 9 5 0 # [3,] 9 5 0 

3 Comments

And to get dimnames on the result: outer(setNames(x,x), setNames(y,y), fun, df=data)
Nice, is vec the vectorize.args argument of Vectorize ?
@user3375672 yes, R functions will match arguments with partial matching
3

You can use Curry and mapply:

library(functional) df = expand.grid(c(1,2,0),c(-1,2,0)) mapply(Curry(fun, df=data), df[,1],df[,2]) #[1] 9 9 7 0 0 0 5 5 3 

5 Comments

mapply is NOT what I want: I need all combinations of x and y, ie a 3x3 matrix (9 results)
in this case, just give mapply the right arguments ;)
Anyone? Seems to close in!
Maybe you want to put the result in a matrix (like outer would do)?
Yes! But I believed outer would do this - if put rightly as in @LegalizeIt's answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.