looking for a more efficient way to create a dataframe than looping in R
r
Solution
An alternative answer using base R functions:
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
Problem
I am trying to create a table/dataframe based on the AcceptanceSampling library as follows: ``` 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 ? Thanks in advance Pete