How can I increase the speed of the following loop operation, if the actual loop range is in 1000's? In the code below:
DF3= Dataframe
OP and k are two columns in DF3 dataframe. Here, k takes a value from 1 to 10.
l1 <- seq(1, 10, 1)
E<-matrix(data=0, nrow=10, ncol=10)
for (i in seq_along(l1)){
for (j in seq_along(l1)){
E[i,j]=sum(ifelse (DF3$OP[DF3$k==i]<DF3$OP[DF3$k==j],1,0))
}
}
DF3 example:
k OP
1 60
1 30
1 38
1 46
2 29
2 35
2 13
2 82
3 100
3 72
3 63
3 45
seq(1, 2552, 1)
, you can use1:2552
; (2) Instead ofseq_along(l1)
, you can just usel1
; (3), Instead ofsum(ifelse ( ... ,1,0))
, just drop theifelse()
function from it because summing logical TRUE/FALSE values is the same as summing 1's and 0's.