gpt4 book ai didi

用 expand.grid 替换嵌套循环并使用多个参数调用内部函数

转载 作者:行者123 更新时间:2023-12-04 18:34:47 26 4
gpt4 key购买 nike

我想用rollapply具有 width 的各种组合的函数, byFUN参数( widthby 应该具有相同的值)。我受到启发 here并创建了以下有效但与 rollapply 无关的代码到目前为止,它只是演示了如何将几个参数传递给 apply 内部的函数。 :

> dframe   <- expand.grid(c(1,2,3), c(1,2,3))
> testFunc <- function(a, b) a^2 + b
> apply(dframe, 1, function(x) testFunc(x[1], x[2]))
[1] 2 5 10 3 6 11 4 7 12
> apply(dframe, 1, function(x) x[1]^2 + x[2])
[1] 2 5 10 3 6 11 4 7 12
> apply(dframe, 1, function(x) (x[1]^2 + x[2]))
[1] 2 5 10 3 6 11 4 7 12
> apply(dframe, 1, function(x) {x[1]^2 + x[2]})
[1] 2 5 10 3 6 11 4 7 12

我的最终解决方案在这里,但这不起作用:
> dframe   <- expand.grid(c(1,2,3), c(median, mean))
> testFunc <- function(a, b) rollapply(mtcars, width = a, by = a, FUN = b, align="left")

> apply(dframe, 1, function(x) testFunc(x[1], x[2]))
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'b' of mode 'function' was not found

> apply(dframe, 1, function(x) rollapply(mtcars, width = x[1], by = x[1], FUN = x[2], align="left"))
Error in match.fun(FUN) : 'x[2]' is not a function, character or symbol

当我打电话时 testFunc直接一切正常,所以我想问题在于 apply无法以某种方式收集结果:
> testFunc(10,mean)
mpg cyl disp hp drat wt qsec vs am gear carb
[1,] 20.37 5.8 208.61 122.8 3.538 3.1280 18.581 0.6 0.3 3.6 2.5
[2,] 19.89 6.6 259.25 149.6 3.552 3.6689 18.301 0.4 0.3 3.4 2.9
[3,] 20.39 6.2 228.25 152.6 3.654 2.8633 16.914 0.3 0.5 3.9 2.6

> class(testFunc(10,mean))
[1] "matrix"

我也试过调试 testFunc并从 apply 调用它并且似乎参数传递正确:
> debug(testFunc)
> apply(dframe, 1, function(x) testFunc(x[1], x[2]))
debugging in: testFunc(x[1], x[2])
debug: rollapply(mtcars, width = a, by = a, FUN = b, align = "left")

Browse[2]> print(a)
$Var1
[1] 1

Browse[2]> print(b)
$Var2
function (x, na.rm = FALSE)
UseMethod("median")
<bytecode: 0x08244ffc>
<environment: namespace:stats>

Browse[2]> n
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'b' of mode 'function' was not found

问题:
  • 什么是错误,我做错了什么?
  • 如何用 expand.grid 替换嵌套循环并调用内部
    具有多个参数的函数?
  • 如何使用 *apply 返回矩阵列表函数族?

  • PS:我想使用两个嵌套循环很容易实现这一点,但我想知道是否有 R-way。

    缴费灵: Here是关于类似错误 ( object 'b' of mode 'function' was not found ) 的答案,结论是 b (在我的情况下)与其他函数的命名参数冲突。但是我在我的代码中看不到这个问题。

    最佳答案

    假设 df <- data.frame(a = 1:2, b = 3:4)我们申请 apply(df, 1, function(x) fun(x)) .然后传递的两个参数 x是向量 c(1, 3)c(2, 4) .

    然而,当 df <- expand.grid(c(1,2,3), c(median, mean))apply(df, 1, function(x) fun(x))完成,我们不能再存储,例如,1median到单个向量,因为它们的类型太不同了。然后x恰好是一个列表,例如,x <- list(1, median) .然后,做 x[1]x[2]不给1median如预期的;相反,这些是具有单个元素的列表(因此出现错误 object 'b' of mode 'function' was not found)。这实际上可以在您的调试示例中看到。

    所以,这里有一些使用 apply 的方法在你的情况下:

    1) 不修改testFunc但要认识到列表是由 apply 传递的;在那种情况下 do.call有帮助,但它也关心 df 的列名,所以我也使用 unname :

    apply(unname(df), 1, do.call, what = testFunc)

    2) 同 1) 但没有 do.call :
    apply(dframe, 1, function(x) testFunc(x[[1]], x[[2]]))

    3) testFunc重新定义为只有一个参数:
    testFunc <- function(a) rollapply(mtcars, width = a[[1]], by = a[[1]], FUN = a[[2]], align="left")
    apply(dframe, 1, testFunc)

    关于用 expand.grid 替换嵌套循环并使用多个参数调用内部函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36018299/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com