R:将args传递给outer()

use*_*672 3 combinations r dplyr

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)
}
Run Code Online (Sandbox Code Playgroud)

这适用于x和y的单个值:

fun(x=1,y=0,df=data)
Run Code Online (Sandbox Code Playgroud)

我想使用outer()(或类似的)来做x和y的组合,但无法弄清楚如何传递df参数.它似乎与此处的问题相同: 使用具有多变量函数的outer().但通过df通过...不起作用:

outer(x=c(0,2),y=c(0,2),fun,df=data)
Run Code Online (Sandbox Code Playgroud)

缺什么 ?

Fra*_*ank 5

我建议使用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
Run Code Online (Sandbox Code Playgroud)

这可能与OP所追求的不等式不匹配,但我怀疑一些变化可以解决这个问题.需要注意的是xy待排序的cut工作.