lundi 25 décembre 2017

R : Loop process takes so long, how can I inhance the speed of loop process?

#for one
setwd("C:/Users/jeongah/Desktop")
select_index <- read.csv("select_index.csv", header = TRUE)
select1_sum <- 0
for (j in 1:5205){

  select_index[j,16]<- 0
  select1_sum <- 0
for(i in 1:15){
  select1_sum <- select1_sum + select_index[j,i]
}
  select1<-(select1_sum/15)
  select_index[j,16]<- select1
}

#for two-------------------17
select2_sum <- 0
select2_subsum <- 0
for (j in 1:5205){

  select_index[j,17] <- 0 # 초기화
  select2 <- 0 # 초기화
   for(i in 1:14){
    select2_subsum <- 0 # 초기화
    for(k in i+1:15){
      select2_subsum <- select_index[j,i] # subsum 초기값 설정
      denon <- 1  # 분모 초기화
      if(select_index[j,i] <= select_index[j,k]){
        denon <- denon+1
        select2_subsum <- select2_subsum+select_index[j,k] # 분모 1 더해고 분자도 더해줌
        select2_sum <- select2_sum + (select2_subsum/denon)       # 평균낼 선택조합을 더함
      }

            else{
        select2_subsum <- select2_subsum # 그대로 
        select2_sum <- select2_sum + (select2_subsum/denon)       # 평균낼 선택조합을 더함
      }



    }
  }
  select2 <-(select2_sum*0.5/choose(15,2)) # 품목 수준으로 평균내기
  select_index[j,17]<- select2
  select2_sum <- 0 # 선택조합의 합 초기화
}



#for three---------------------------------------------------------------------------------------------18-----------

select3_sum <- 0
select3_subsum <- 0
select3_compare <- 0
j <- 0
i <- 0
k <- 0
l <- 0
for (j in 1:5025){

  select_index[j,18] <- 0 # 초기화
  select3 <- 0 # 초기화
  for(i in 1:13){
    select3_subsum <- 0 # 초기화
    denon <- 1  # 분모 초기화
    for(k in i+1:14){

      select3_compare <- select_index[j,i]
      select3_subsum <- select3_compare # subsum 초기값 설정

      for(a in 3:15){


       if(select3_compare <= select_index[j,a]){
         select3_compare <- select_index[j,a]
         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 # 분모 1 더해고 분자도 더해줌
      }
      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 # 선택조합의 합 초기화
}

# for four -----------------------------------------------------------------------------19

select4_sum <- 0
select4_subsum <- 0
select4_compare <- 0
j <- 0
i <- 0
k <- 0
a <- 0
b <- 0
for (j in 1:5025){

  select_index[j,19] <- 0 # 초기화
  select4 <- 0 # 초기화
  for(i in 1:12){
    select4_subsum <- 0 # 초기화
    denon <- 1  # 분모 초기화

    for(k in i+1:13){
      select4_compare <- select_index[j,i]
      select4_subsum <- select4_compare # subsum 초기값 설정

      for(a in 3:14){

        for(b in 4:15){

          if(select4_compare <= select_index[j,b]){
            select4_compare <- select_index[j,b]
            denon <- denon+1
            select4_subsum <- select4_subsum+select4_compare
          }
          select4_sum <- select4_sum + (select4_subsum/denon)
        }

        }


        if(select4_compare <= select_index[j,a]){
          select4_compare <- select_index[j,a]
          denon <- denon+1
          select4_subsum <- select4_subsum+select4_compare
        }
        select4_sum <- select4_sum + (select4_subsum/denon)
      }

      if(select4_compare <= select_index[j,k]){
        select4_compare <- select_index[j,k]
        denon <- denon+1
        select4_subsum <- select4_subsum+select4_compare # 분모 1 더해고 분자도 더해줌
      }
      else{
        select4_subsum <- select4_subsum # 그대로 둠
      }

      select4_sum <- select4_sum + (select4_subsum/denon) # 평균낼 선택조합을 더함
    }
  }
  select4 <-(select4_sum/choose(15,4)) # 품목 수준으로 평균내기
  select_index[j,19]<- select4
  select4_sum <- 0 # 선택조합의 합 초기화


  # for five-----------------------------------------------------------------------------------20

  select5_sum <- 0
  select5_subsum <- 0
  select5_compare <- 0
  j <- 0
  i <- 0
  k <- 0
  a <- 0
  b <- 0
  for (j in 1:5025){

    select_index[j,20] <- 0 # 초기화
    select5 <- 0 # 초기화
    for(i in 1:12){
      select5_subsum <- 0 # 초기화
      denon <- 1  # 분모 초기화

      for(k in i+1:13){
        select5_compare <- select_index[j,i]
        select5_subsum <- select5_compare # subsum 초기값 설정

        for(a in 3:14){

          for(b in 4:15){

            for(c in 5:15){
              if(select5_compare <= select_index[j,c]){
                select5_compare <- select_index[j,c]
                denon <- denon+1
                select5_subsum <- select5_subsum+select5_compare
              }
              select5_sum <- select5_sum + (select5_subsum/denon)
            }


            if(select5_compare <= select_index[j,b]){
              select5_compare <- select_index[j,b]
              denon <- denon+1
              select5_subsum <- select5_subsum+select5_compare
            }
            select5_sum <- select5_sum + (select5_subsum/denon)
          }

        }


        if(select5_compare <= select_index[j,a]){
          select5_compare <- select_index[j,a]
          denon <- denon+1
          select5_subsum <- select5_subsum+select5_compare
        }
        select5_sum <- select5_sum + (select5_subsum/denon)
      }

      if(select5_compare <= select_index[j,k]){
        select5_compare <- select_index[j,k]
        denon <- denon+1
        select5_subsum <- select5_subsum+select5_compare # 분모 1 더해고 분자도 더해줌
      }
      else{
        select5_subsum <- select5_subsum # 그대로 둠
      }

      select5_sum <- select5_sum + (select5_subsum/denon) # 평균낼 선택조합을 더함
    }
  }
  select5 <-(select5_sum/choose(15,5)) # 품목 수준으로 평균내기
  select_index[j,20]<- select5
  select5_sum <- 0 # 선택조합의 합 초기화

It seems this code has no error, but it takes so long. I've heard there is a method to get the loop process faster. would you give me a hand? I wish I could upload the data but it's too big to upload. Full codes are stretched to the cases of 15 countries selection.

let me specify my question, if there are 15 countries, "a, b, c ... o". each countries has index value for each product from 2 to 7. and For product 1(there is no label for it, just numeric value), if we do with 3 countries, country A has 2, country B has 7 and country C has 4. the calculation process is like this. the first value would be 2. and the value of B is higher than A, so their average would be 2+7/2. but the value of C is lower than B so it would be thrown away. the number of cases according to the number of selected countries is 15 C(Combination) n The all calculation cases would be 2^15-1.

Aucun commentaire:

Enregistrer un commentaire