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
