我想制作代码以根据1到15的数量选择国家并累积相同或以上的值。
这是我为三个国家的案例所做的。但我想做一个更通用的,它不必为每个国家制定单独的代码。
select3_sum <- 0
select3_subsum <- 0
select3_compare <- 0
for (j in 1:5025){
select_index[j,18] <- 0
select3 <- 0
for(i in 1:13){
select3_subsum <- 0
for(k in i+1:14){
select3_compare <- select_index[j,i]
select3_subsum <- select3_compare
for(a in k+1:15){
denon <- 1
if(select3_compare <= select_index[a,l]){
select3_compare <- select_index[a,l]
denon <- denon+1
select3_subsum <- select3_subsum+select3_compare
}
select3_sum <- select3_sum + (select3_subsum/denon)
}
if(select3_compare <= select_index[j,k]){
select3_compare <- select_index[j,k]
denon <- denon+1
select3_subsum <- select3_subsum+select3_compare
}
else{
select3_subsum <- select3_subsum
}
select3_sum <- select3_sum + (select3_subsum/denon)
}
}
select3 <-(select3_sum/choose(15,3))
select_index[j,18]<- select3
select3_sum <- 0
数据如下。df select_index 太大,无法在这里上传,所以我只取了一小部分。
structure(list(V1 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("6",
"CHILE.index"), class = "factor"), V2 = structure(c(2L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("6", "SINGAPORE.index"), class = "factor"),
V3 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7",
"EFTA.index"), class = "factor"), V4 = structure(c(2L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("6", "USA.index"), class = "factor"),
V5 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7",
"ASEAN.index"), class = "factor"), V6 = structure(c(2L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("7", "INDIA.index"), class = "factor"),
V7 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7",
"EU.index"), class = "factor"), V8 = structure(c(2L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("7", "PERU.index"), class = "factor"),
V9 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7",
"TURKEY.index"), class = "factor"), V10 = structure(c(2L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", "COLOMBIA.index"
), class = "factor"), V11 = structure(c(2L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("7", "AUSTRAILIA.index"), class = "factor"),
V12 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("6",
"CANADA.index"), class = "factor"), V13 = structure(c(2L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", "NEW ZEALAND.index"
), class = "factor"), V14 = structure(c(2L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("7", "VIETNAM.index"), class = "factor"),
V15 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7",
"CHINA.index"), class = "factor"), V16 = structure(c(2L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", "select1"), class = "factor"),
V17 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0",
"select2"), class = "factor"), V18 = structure(c(2L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("0", "select3"), class = "factor"),
V19 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0",
"select4"), class = "factor"), V20 = structure(c(2L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("0", "select5"), class = "factor"),
V21 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0",
"select6"), class = "factor"), V22 = structure(c(2L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("0", "select7"), class = "factor"),
V23 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0",
"selec8"), class = "factor"), V24 = structure(c(2L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("0", "selec9"), class = "factor"),
V25 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0",
"selec10"), class = "factor"), V26 = structure(c(2L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("0", "selec11"), class = "factor"),
V27 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0",
"selec12"), class = "factor"), V28 = structure(c(2L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("0", "selec13"), class = "factor"),
V29 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0",
"selec14"), class = "factor"), V30 = structure(c(2L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("0", "selec15"), class = "factor")), .Names = c("V1",
"V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11",
"V12", "V13", "V14", "V15", "V16", "V17", "V18", "V19", "V20",
"V21", "V22", "V23", "V24", "V25", "V26", "V27", "V28", "V29",
"V30"), class = "data.frame", row.names = c(NA, -7L))
对于国家数量为一和二的情况,我已经得到了结果。结果如下。
structure(list(X = 1:3, CHILE.index = c(6L, 6L, 6L), SINGAPORE.index = c(6L,
6L, 6L), EFTA.index = c(7L, 7L, 7L), USA.index = c(6L, 6L, 6L
), ASEAN.index = c(7L, 7L, 7L), INDIA.index = c(7L, 7L, 7L),
EU.index = c(7L, 7L, 7L), PERU.index = c(7L, 7L, 7L), TURKEY.index = c(7L,
7L, 7L), COLOMBIA.index = c(7L, 7L, 7L), AUSTRAILIA.index = c(7L,
7L, 7L), CANADA.index = c(6L, 6L, 6L), NEW.ZEALAND.index = c(7L,
7L, 7L), VIETNAM.index = c(7L, 7L, 7L), CHINA.index = c(7L,
7L, 7L), select1 = c(6.733333333, 6.733333333, 6.733333333
), select2 = c(6.804603175, 6.804603175, 6.804603175), select3 = c(0L,
0L, 0L), select4 = c(0L, 0L, 0L), select5 = c(0L, 0L, 0L),
select6 = c(0L, 0L, 0L), select7 = c(0L, 0L, 0L), selec8 = c(0L,
0L, 0L), selec9 = c(0L, 0L, 0L), selec10 = c(0L, 0L, 0L),
selec11 = c(0L, 0L, 0L), selec12 = c(0L, 0L, 0L), selec13 = c(0L,
0L, 0L), selec14 = c(0L, 0L, 0L), selec15 = c(0L, 0L, 0L)), .Names = c("X",
"CHILE.index", "SINGAPORE.index", "EFTA.index", "USA.index",
"ASEAN.index", "INDIA.index", "EU.index", "PERU.index", "TURKEY.index",
"COLOMBIA.index", "AUSTRAILIA.index", "CANADA.index", "NEW.ZEALAND.index",
"VIETNAM.index", "CHINA.index", "select1", "select2", "select3",
"select4", "select5", "select6", "select7", "selec8", "selec9",
"selec10", "selec11", "selec12", "selec13", "selec14", "selec15"
), class = "data.frame", row.names = c(NA, -3L))
如何编写代码来计算所有案例(从 1 到 15 的国家数量)而不会太复杂?
让我具体说明我的问题,如果有 15 个国家,“a、b、c ... o”。每个国家的每个产品都有从 2 到 7 的索引值。对于产品 1(没有标签,只有数值),如果我们处理 3 个国家,A 国有 2,B 国有 7,C 国有4.计算过程是这样的。第一个值为 2。B 的值高于 A,因此它们的平均值为 2+7/2。但是 C 的值比 B 低,所以它会被扔掉。根据所选国家的数量计算的案例数量为 15 C(组合) n 所有计算案例将为 2^15-1。
感谢你所做的一切。