3730 18

[问答] R语言中批量计算不同参数取值下函数值的函数? [推广有奖]

11楼
大洼村二号机 发表于 2017-2-6 22:04:03
zhangyangsmith 发表于 2017-2-6 21:38
mapply
mapply可以实现一个函数中某一个参数在不同取值下的值,
有没有便捷的函数实现两个或两个以上的参数在不同的取值下的函数值的?最后算出来一个表格矩阵那样的。

12楼
大洼村二号机 发表于 2017-2-6 22:04:07
zhangyangsmith 发表于 2017-2-6 21:38
mapply
mapply可以实现一个函数中某一个参数在不同取值下的值,
有没有便捷的函数实现两个或两个以上的参数在不同的取值下的函数值的?最后算出来一个表格矩阵那样的。

13楼
大洼村二号机 发表于 2017-2-6 22:04:08
zhangyangsmith 发表于 2017-2-6 21:38
mapply
mapply可以实现一个函数中某一个参数在不同取值下的值,
有没有便捷的函数实现两个或两个以上的参数在不同的取值下的函数值的?最后算出来一个表格矩阵那样的。

14楼
zhangyangsmith 发表于 2017-2-6 22:16:12
Any example?

15楼
大洼村二号机 发表于 2017-2-7 09:48:35
zhangyangsmith 发表于 2017-2-6 22:16
Any example?
我现在写好了一个函数,函数里面有缴费率C、投资收益率R、退休年龄B三个参数,目的是计算C\R\B取不同的值的时候函数的值,
C<-seq(0.06,0.16,by=0.01),R<-seq(0.04,0.14,by=0.01),B<-seq(55,65,by=0.01)
我希望得到的结果大概是这样的:
当B=55时

16楼
大洼村二号机 发表于 2017-2-7 09:50:37
刚刚图片发失败了,想通过程序得到如附件这样的计算效果,不知道应该用什么函数实现

1.png (2.49 KB)

1.png

17楼
zhangyangsmith 发表于 2017-2-7 20:51:09
  1. # Suppose your function has the following declaration: myfun <- function(c, r, b) {...}
  2. C<-seq(0.06,0.16,by=0.01)
  3. R<-seq(0.04,0.14,by=0.01)
  4. B<-seq(55,65,by=0.01)
  5. lapply( B, function(d) outer( C, R, function(x, y) myfun(c = x, r = y, b = d) ) )
复制代码
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
大洼村二号机 + 1 + 1 + 1 热心帮助其他会员

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

18楼
大洼村二号机 发表于 2017-2-8 09:19:17
zhangyangsmith 发表于 2017-2-7 20:51
完!美!试了一下果然是可行的!
不过不太懂这么写的意思,outer在这里的用法是相当于新建了一个函数d,在函数d中嵌套了myfun函数么?
如果要新增一个参数构成四维甚至多维矩阵的话也可以使用这种嵌套函数的写法么?

19楼
zhangyangsmith 发表于 2017-2-8 21:04:42
大洼村二号机 发表于 2017-2-8 09:19
完!美!试了一下果然是可行的!
不过不太懂这么写的意思,outer在这里的用法是相当于新建了一个函数d, ...
Help on "outer" function:
Description
The outer product of the arrays X and Y is the array A with dimension c(dim(X), dim(Y)) where element A[c(arrayindex.x, arrayindex.y)] = FUN(X[arrayindex.x], Y[arrayindex.y], ...).

Usage
outer(X, Y, FUN = "*", ...)
Basically it just creates all possible combinations of two vectors (denoted "X" and "Y" above) and call the *binary* function (denoted "FUN" above) on each and every of the combination.

But as we have a 3-argument function here I just loop through the 3rd dimension using "lapply". In "lapply", for each value "d" in vector "B" we find all possible combinations of the elements in vectors C and R call the function with one of the combinations and "d".

For cases with more than 3 dimensions, you can certainly just put another or several other "lappy" outside the code. But I would not recommend that. If you have a similar question but in much higher dimension and it is fine to have results stored in a 2-d table, perhaps the following code is more clear (back to my original suggestion of using "mapply"):
  1. # Suppose we have a four-argument function: myfun <- function(w, x, y, z)
  2. # And we have four vectors: A, B, C, D from whose values should be passed to arguments w, x, y, and z, respectively.

  3. # Create a data.frame for all combinations of elements in A, B, C and D
  4. mydf <- Reduce( merge, list( data.frame(A = A), data.frame(B = B), data.frame(C = C), data.frame(D = D) ) )

  5. mydf$res <- with( mydf, mapply( myfun, w = A, x = B, y = C, z = D ) )
复制代码
The code was not tested though. Let me know if you have any issues running it.




您需要登录后才可以回帖 登录 | 我要注册

客服
GMT+8, 2026-12-2 08:08