首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在一些参数是常量而另一些参数不是常量时应用函数

在一些参数是常量而另一些参数不是常量时应用函数
EN

Stack Overflow用户
提问于 2018-04-22 03:17:29
回答 1查看 25关注 0票数 0

我需要计算CVfromCI。在这个函数中,参数lowerupperpen是不同的;参数designalpharobust是常量。我怎样才能使我的代码更短?现在我需要从头到尾每次都要写。

代码语言:javascript
复制
library(PowerTOST)

CVfromCI(pe = 0.95, lower = 0.86, upper = 1.029, n = 24, design = "2x2", alpha = 0.05, robust = FALSE)
CVfromCI(pe = 0.94, lower = 0.897, upper = 1.027, n = 24, design = "2x2", alpha = 0.05, robust = FALSE)
CVfromCI(pe = 0.99, lower = 0.88, upper = 1.025, n = 24, design = "2x2", alpha = 0.05, robust = FALSE)
EN

回答 1

Stack Overflow用户

发布于 2018-04-24 17:31:14

我们可以使用mapply来应用带有多个参数的函数CVfromCI

代码语言:javascript
复制
library(PowerTOST)

mapply(CVfromCI, 
       pe = c(0.95, 0.94, 0.99),
       lower = c(0.86, 0.897, 0.88),
       upper = c(1.029, 1.027, 1.025),
       n = 24,
       design = "2x2",
       alpha = 0.05,
       robust = FALSE)
# [1] 0.1824596 0.1371548 0.1547650
# Warning messages:
# 1: sigma based on pe & lower CL more than 10% different than
# sigma based on pe & upper CL. Check input. 
# 2: sigma based on pe & lower CL more than 10% different than
# sigma based on pe & upper CL. Check input. 
# 3: sigma based on pe & lower CL more than 10% different than
# sigma based on pe & upper CL. Check input.

我们也可以使用purrr包中的pmap_dbl。请注意,在使用pmap_dbl时,我们首先以列表的形式提供多个参数,然后提供函数。

代码语言:javascript
复制
library(purrr)

pmap_dbl(list(pe = c(0.95, 0.94, 0.99),
              lower = c(0.86, 0.897, 0.88),
              upper = c(1.029, 1.027, 1.025),
              n = 24,
              design = "2x2",
              alpha = 0.05,
              robust = FALSE),
         CVfromCI)
# [1] 0.1824596 0.1371548 0.1547650
# Warning messages:
# 1: sigma based on pe & lower CL more than 10% different than
# sigma based on pe & upper CL. Check input. 
# 2: sigma based on pe & lower CL more than 10% different than
# sigma based on pe & upper CL. Check input. 
# 3: sigma based on pe & lower CL more than 10% different than
# sigma based on pe & upper CL. Check input.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49959390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档