Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 在某些参数为常量而某些参数为非常量时应用函数_Loops_For Loop_Lapply_Purrr - Fatal编程技术网

Loops 在某些参数为常量而某些参数为非常量时应用函数

Loops 在某些参数为常量而某些参数为非常量时应用函数,loops,for-loop,lapply,purrr,Loops,For Loop,Lapply,Purrr,我需要计算CVfromCI。在这个函数中,参数lower、upper、pe和n是不同的;参数design、alpha和robust都是常量。如何缩短代码?现在我需要从开始到结束每次都写 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,

我需要计算CVfromCI。在这个函数中,参数
lower
upper
pe
n
是不同的;参数
design
alpha
robust
都是常量。如何缩短代码?现在我需要从开始到结束每次都写

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)

我们可以使用
mapply
将函数
CVfromCI
应用于多个参数

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.
我们还可以使用包中的
pmap\u dbl
。请注意,在使用
pmap_dbl
时,我们首先提供多个参数作为列表,然后提供函数

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.