[英]looking for a more efficient way to create a dataframe than looping in R
I am trying to create a table/dataframe based on the AcceptanceSampling library as follows: 我正在尝试基于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")
this gives me: 这给了我:
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
Can someone point to a more efficient way to build this ? 有人能指出一种更有效的方法来构建它吗? Preferably without the loops and without having to call find.plan() twice for each row ? 最好没有循环而不必为每一行调用find.plan()两次?
Thanks in advance Pete 提前谢谢皮特
You can use expand.grid
like this : 您可以像这样使用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))
Then using data.table
for each syntax sugar: 然后使用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
EDIT using base functions, the idea is to vectorize find.plan
. 编辑使用基本函数,想法是矢量化find.plan
。 Here I am using mapply
like this: 在这里我使用这样的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
An alternative answer using base R functions: 使用基本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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.