dimanche 24 décembre 2017

R : choosing from 1 to 15 countries and accumulate the value if it is same or over

I'd like to make codes to choose countries according to the number of 1 to 15 and accumulate the value if it is same or over.

Here is what I'm doing for the cases of three countries. But I'd like to make more general one, which doesn't have to make individual codes for each number of countries.

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 

data is like below

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))
I'd like to calculate the average of accumulation of index(accumulate in case it is same or over, if not then it is not included in calculation)

I've already got results for cases when the number of countries is one and two. the results is like below.

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))

How can I make codes to calculate all cases(the number of countries from 1 to 15) without being too complex?

thank you for all.

Aucun commentaire:

Enregistrer un commentaire