我正在努力完成我的循环性能,目前需要超过30小时才能完成。我附上代码和信息。我认为是由于我使用了很多数据帧操作,但我不知道如何以其他方式做到这一点。 range_six_months
有900k行,copia_grupos_2
有4078k行
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
}
}
在循环之前: Screenshot of copia_grupos_2 Screenshot of range_six_months
循环后: Expected Output