寻找一种比在R中循环更有效的方法来创建数据帧

PJP*_*PJP 3 r

我正在尝试基于AcceptanceSampling库创建表/数据框,如下所示:

library(AcceptanceSampling)
df<-NULL
for (aql in c(0.01,0.05)){
for (prp in c(0.95)) {
for (def in c(0.06,0.1,0.15)){
for (crp in c(0.05,0.08,0.10)){
df<-as.data.frame(rbind(df,c(aql,prp,def,crp,
                         find.plan(PRP=c(aql,prp),CRP=c(def,crp))$n,
                         find.plan(PRP=c(aql,prp),CRP=c(def,crp))$c
                         )))
}}}}

names(df)<-c("aql","prp","def","crp","n","Ac")
Run Code Online (Sandbox Code Playgroud)

这给了我:

    aql  prp  def  crp    n  Ac
1  0.01 0.95 0.06 0.05  127   3
2  0.01 0.95 0.06 0.08  116   3
3  0.01 0.95 0.06 0.10  110   3
4  0.01 0.95 0.10 0.05   61   2
5  0.01 0.95 0.10 0.08   55   2
6  0.01 0.95 0.10 0.10   52   2
7  0.01 0.95 0.15 0.05   30   1
8  0.01 0.95 0.15 0.08   27   1
9  0.01 0.95 0.15 0.10   25   1
10 0.05 0.95 0.06 0.05 5626 308
11 0.05 0.95 0.06 0.08 4826 266
12 0.05 0.95 0.06 0.10 4445 246
13 0.05 0.95 0.10 0.05  298  21
14 0.05 0.95 0.10 0.08  251  18
15 0.05 0.95 0.10 0.10  233  17
16 0.05 0.95 0.15 0.05   93   8
17 0.05 0.95 0.15 0.08   79   7
18 0.05 0.95 0.15 0.10   77   7
Run Code Online (Sandbox Code Playgroud)

有人能指出一种更有效的方法来构建它吗?最好没有循环而不必为每一行调用find.plan()两次?

提前谢谢皮特

ags*_*udy 5

你可以expand.grid像这样使用:

dat <- expand.grid(aql = c(0.01,0.05),prp = c(0.95),
            def = c(0.06,0.1,0.15), crp = c(0.05,0.08,0.10))
Run Code Online (Sandbox Code Playgroud)

然后使用data.table每个语法糖:

library(data.table)
DT <- as.data.table(dat)
DT[, c('n','Ac') := list(find.plan(PRP=c(aql,prp),CRP=c(def,crp))$n,
                         find.plan(PRP=c(aql,prp),CRP=c(def,crp))$c),
                           by = 1:nrow(DT)]

    aql  prp  def  crp    n  Ac
 1: 0.01 0.95 0.06 0.05  127   3
 2: 0.05 0.95 0.06 0.05 5626 308
 3: 0.01 0.95 0.10 0.05   61   2
 4: 0.05 0.95 0.10 0.05  298  21
 5: 0.01 0.95 0.15 0.05   30   1
 6: 0.05 0.95 0.15 0.05   93   8
 7: 0.01 0.95 0.06 0.08  116   3
 8: 0.05 0.95 0.06 0.08 4826 266
 9: 0.01 0.95 0.10 0.08   55   2
10: 0.05 0.95 0.10 0.08  251  18
11: 0.01 0.95 0.15 0.08   27   1
12: 0.05 0.95 0.15 0.08   79   7
13: 0.01 0.95 0.06 0.10  110   3
14: 0.05 0.95 0.06 0.10 4445 246
15: 0.01 0.95 0.10 0.10   52   2
16: 0.05 0.95 0.10 0.10  233  17
17: 0.01 0.95 0.15 0.10   25   1
18: 0.05 0.95 0.15 0.10   77   7
Run Code Online (Sandbox Code Playgroud)

编辑使用基本功能,想法是矢量化find.plan.我在这里使用mapply这样:

 cbind(dat,with(dat,t(mapply(function(x,y,z,t)
                 find.plan(c(x,y),c(z,t)),aql,prp,def,crp))))

  aql  prp  def  crp    n   c   r
1  0.01 0.95 0.06 0.05  127   3   4
2  0.05 0.95 0.06 0.05 5626 308 309
3  0.01 0.95 0.10 0.05   61   2   3
4  0.05 0.95 0.10 0.05  298  21  22
5  0.01 0.95 0.15 0.05   30   1   2
6  0.05 0.95 0.15 0.05   93   8   9
7  0.01 0.95 0.06 0.08  116   3   4
8  0.05 0.95 0.06 0.08 4826 266 267
9  0.01 0.95 0.10 0.08   55   2   3
10 0.05 0.95 0.10 0.08  251  18  19
11 0.01 0.95 0.15 0.08   27   1   2
12 0.05 0.95 0.15 0.08   79   7   8
13 0.01 0.95 0.06 0.10  110   3   4
14 0.05 0.95 0.06 0.10 4445 246 247
15 0.01 0.95 0.10 0.10   52   2   3
16 0.05 0.95 0.10 0.10  233  17  18
17 0.01 0.95 0.15 0.10   25   1   2
18 0.05 0.95 0.15 0.10   77   7   8
Run Code Online (Sandbox Code Playgroud)


the*_*ail 5

使用基本R函数的替代答案:

install.packages("AcceptanceSampling")
library(AcceptanceSampling)

df <- expand.grid(
  aql = c(0.01,0.05),
  prp = c(0.95),
  def = c(0.06,0.1,0.15),
  crp = c(0.05,0.08,0.10)
)

findpl <- do.call(
  rbind,
  by(df,df,function(x) {
    i <- find.plan(c(x$aql,x$prp),c(x$def,x$crp))
    c(n=i$n,Ac=i$c)
    }
  )
)

result <- data.frame(df,findpl)

head(result)

   aql  prp  def  crp    n  Ac
1 0.01 0.95 0.06 0.05  127   3
2 0.05 0.95 0.06 0.05 5626 308
3 0.01 0.95 0.10 0.05   61   2
4 0.05 0.95 0.10 0.05  298  21
5 0.01 0.95 0.15 0.05   30   1
6 0.05 0.95 0.15 0.05   93   8
Run Code Online (Sandbox Code Playgroud)