![](/img/trans.png)
[英]R combinations, looking for faster and more efficient way(package,code,parallel cpu) than basic R
[英]looking for a more efficient way to create a dataframe than looping in 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")
這給了我:
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
有人能指出一種更有效的方法來構建它嗎? 最好沒有循環而不必為每一行調用find.plan()兩次?
提前謝謝皮特
您可以像這樣使用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))
然后使用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
編輯使用基本函數,想法是矢量化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
使用基本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.