0

I am struggling with the performance of my loop, currently is taking more than 30h to finish. I attach the code and info.I think is due to i am using a lot of operations of dataframes but i dont know how to do it in other way. range_six_months has 900k rows and copia_grupos_2 has 4078k rows

client<- numeric(nrow(range_six_months))
client<- range_six_months$CustomerIdx
bond <- numeric(nrow(range_six_months))
bond <- range_six_months$IsinIdx
i<-19
for (i in 1:nrow(range_six_months)){
  aaa <- copia_grupos_2[copia_grupos_2$CustomerIdx == client[i] & copia_grupos_2$IsinIdx == bond [i], c(2,3,4,6)]
  for (j in 1:nrow(aaa)){
    actual <- aaa$week[j]
    interes <- sum(aaa$CustomerInterest[aaa$week >= actual - 26 & aaa$week< actual])
    copia_grupos_2$num_interest[copia_grupos_2$CustomerIdx == client[i] &
                                  copia_grupos_2$IsinIdx == bond [i] &
                                  copia_grupos_2$week== actual] <- interes
  }
}

Beforee the loop: Screenshot of copia_grupos_2 Screenshot of range_six_months

After the loop: Expected Output

  • 4
    Please provide [example input data](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and expected output, maybe there is no need for *forloops*. – zx8754 Jun 13 '18 at 10:52
  • 1
    did you read http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example ?? do not post jpgs – minem Jun 13 '18 at 11:04
  • i already pòsted some images – Enric Rovira Jun 13 '18 at 11:07
  • 2
    @EnricRovira then maybe try to create a reproducible example data as described in linked post – minem Jun 13 '18 at 11:08

0 Answers0