如何用lapply定义多个变量?

jay*_*.sf 6 variables r function lapply

我想将具有不同值的多个变量的函数应用于列表.我知道如何使用一个变化的变量来做到这一点

sapply(c(1:10), function(x) x * 2)
# [1]  2  4  6  8 10 12 14 16 18 20
Run Code Online (Sandbox Code Playgroud)

但不是两个.我首先手动向你展示我想要的东西(实际上我使用 lapply()sapply()在SO中更具概要性):

# manual
a <- sapply(c(1:10), function(x, y=2) x * y)
b <- sapply(c(1:10), function(x, y=3) x * y)
c <- sapply(c(1:10), function(x, y=4) x * y)
c(a, b, c)
# [1]  2  4  6  8 10 12 14 16 18 20  3  6  9 12 15 18 21 24 27 30  4  8 12 
# [24]  16 20 24 28 32 36 40
Run Code Online (Sandbox Code Playgroud)

这是我尝试,我尝试定义都xy.

# attempt
X <- list(x = 1:10, y = 2:4)
sapply(c(1:10, 2:4), function(x, y) x * y)
# Error in FUN(X[[i]], ...) : argument "y" is missing, with no default
Run Code Online (Sandbox Code Playgroud)

解决方案的基准

library(microbenchmark)
microbenchmark(sapply = as.vector(sapply(1:10, function(x, y) x * y, 2:4)), 
               mapply = mapply( FUN = function(x, y) x * y, 1:10, rep( x = 2:4, each = 10)),
               sapply2 = as.vector(sapply(1:10, function(y) sapply(2:4, function(x) x * y))),
               outer = c(outer(1:10, 2:4, function(x, y) x * y)))
# Unit: microseconds
# expr        min       lq      mean   median       uq      max neval
# sapply   34.212  36.3500  62.44864  39.1295  41.9090 2304.542   100
# mapply   62.008  65.8570  87.82891  70.3470  76.5480 1283.342   100
# sapply2 196.714 203.9835 262.09990 223.6550 232.2080 3344.129   100
# outer     7.698  10.4775  13.02223  12.4020  13.4715   53.883   100
Run Code Online (Sandbox Code Playgroud)

Cri*_*uno 6

使用mapply()

mapply() 将函数应用于多个列表或矢量参数.

rep()也用于重复值2,3和4.在each参数中指定10 ,rep()重复x10次​​的每个元素.

这是必要的,因为mapply()- 1:10中的第一个参数长度为10.

# supply the function first, followed by the
# arguments in the order in which they are called in `FUN`
mapply( FUN = function(x, y) x * y
        , 1:10
        , rep( x = 2:4, each = 10)
)

# [1]   2  4  6  8 10 12 14 16 18 20  3  6  9 12 15 18 21 24 27 30  4  8 12 16 20
# [26] 24 28 32 36 40
Run Code Online (Sandbox Code Playgroud)


Jor*_*eys 6

首先,lapply()如果你的函数是矢量化的,你可以这样做.在这种情况下,它是:

x <- 1:10
unlist(lapply(2:4, function(y) x*y))
# OR
unlist(lapply(2:4, function(x=x,y) x*y))
Run Code Online (Sandbox Code Playgroud)

其次,如果需要对两个向量的每个组合应用函数,请使用outer():

xf <- 1:10
yf <- 2:4
c(xf %o% yf)
# OR spelled out for any function:
c(outer(xf,yf,FUN = `*`))
Run Code Online (Sandbox Code Playgroud)

如果使用mapply,则可以使用参数MoreArgs来避免必须使用rep构造参数:

xf <- 1:10
yf <- 2:4
mapply(function(x,y) x*y,
       y = yf,
       MoreArgs = list(x = xf))
Run Code Online (Sandbox Code Playgroud)

这与lapply()我上面显示的构造完全等效.生成的矩阵也可以使用SIMPLIFY = FALSE和转换为矢量unlist():

unlist(mapply(function(x,y) x*y,
              y = yf,
              MoreArgs = list(x = xf),
              SIMPLIFY = FALSE))
Run Code Online (Sandbox Code Playgroud)

哪种解决方案最方便,取决于您的实际用例.时间方面它们都是可比较的,在最近的R版本中可能outer()比其他解决方案稍慢.

标杆

为了说明结果如何根据对象的大小和顺序而大不相同,我包括以下基准测试结果(下面的代码和输出).这表明:

  1. outer() 不一定是最快的解决方案,尽管它通常是最快的解决方案之一.
  2. 手动重复一个向量mapply()会增加很多开销,即使双重sapply()调用也会更快.

代码:警告:这将运行一段时间

fx <- sample(1e4)
fy <- sample(1e3)
library(microbenchmark)
microbenchmark(sapply = as.vector(sapply(fx, function(x, y) x * y, fy)), 
               mapply = mapply( FUN = function(x, y) x * y, fx, rep( fy, each = 1e4)),
               sapply2 = as.vector(sapply(fx, function(y) sapply(fy, function(x) x * y))),
               outer = c(outer(fx, fy, function(x, y) x * y)),
               mapply2 = mapply(function(x,y) x*y, x=fx, MoreArgs = list(y = fy)),
               mapply3 = mapply(function(x,y) x*y, y=fy, MoreArgs = list(x = fx)),
               times = 15)
Run Code Online (Sandbox Code Playgroud)

我机器上的输出:

Unit: milliseconds
    expr         min          lq       mean      median          uq        max neval cld
  sapply    89.52318    92.98653   344.1538   117.11280   239.64887  1485.3178    15 a  
  mapply 20471.02137 22925.42757 24478.5985 24650.29055 25627.31232 28840.3494    15   c
 sapply2  7472.02251  8268.04696  9519.8016  8707.19193  9528.46181 14182.7537    15  b 
   outer    77.62331    85.94651   189.5107    91.83722   182.08506  1119.6620    15 a  
 mapply2    77.76871    79.71924   143.9484    81.24168    84.53247   971.1792    15 a  
 mapply3    65.21709    71.85662   107.9586    73.80779   124.21141   242.0760    15 a  
Run Code Online (Sandbox Code Playgroud)


G. *_*eck 6

一般解决方案

试试outer:

c(outer(1:10, 2:4, Vectorize(function(x, y) x*y)))
##  [1]  2  4  6  8 10 12 14 16 18 20  3  6  9 12 15 18 21 24 27 30  4  8 12 16 20
## [26] 24 28 32 36 40
Run Code Online (Sandbox Code Playgroud)

如果函数已经Vectorized

如果函数已经被矢量化,就像在这里一样,那么我们可以省略Vectorize:

c(outer(1:10, 2:4, function(x, y) x * y))
##  [1]  2  4  6  8 10 12 14 16 18 20  3  6  9 12 15 18 21 24 27 30  4  8 12 16 20
## [26] 24 28 32 36 40
Run Code Online (Sandbox Code Playgroud)

显示的特定示例

实际上,在这种特殊情况下,显示的匿名函数是默认函数,因此这将起作用:

c(outer(1:10, 2:4))
##  [1]  2  4  6  8 10 12 14 16 18 20  3  6  9 12 15 18 21 24 27 30  4  8 12 16 20
## [26] 24 28 32 36 40
Run Code Online (Sandbox Code Playgroud)

在这种特殊情况下我们也可以使用:

c(1:10 %o% 2:4)
##  [1]  2  4  6  8 10 12 14 16 18 20  3  6  9 12 15 18 21 24 27 30  4  8 12 16 20
## [26] 24 28 32 36 40
Run Code Online (Sandbox Code Playgroud)

如果输入是列表X.

如果您的起点是问题中X显示的列表,那么:

c(outer(X[[1]], X[[2]], Vectorize(function(x, y) x * y)))
##  [1]  2  4  6  8 10 12 14 16 18 20  3  6  9 12 15 18 21 24 27 30  4  8 12 16 20
## [26] 24 28 32 36 40
Run Code Online (Sandbox Code Playgroud)

要么

c(do.call("outer", c(unname(X), Vectorize(function(x, y) x*y))))
##  [1]  2  4  6  8 10 12 14 16 18 20  3  6  9 12 15 18 21 24 27 30  4  8 12 16 20
## [26] 24 28 32 36 40
Run Code Online (Sandbox Code Playgroud)

如果适用,前面部分适用于缩短它的地方.