[R] Counter in a For Loop - Efficiency Issue

Leendert Punt leendertp at gmail.com
Wed Feb 2 08:35:28 CET 2011


I have a 32000 x 14 matrix (M) where entry (x, y) corresponds to
person x at a discrete time y. I then have a matrix (M2) of 60000 x 2
where each entry is a an event by a person that is in the above 32000
and at a time that is in the range of the discrete time points above.
I want to populate the another matrix (M3) such that (x, y) is the
number of events of person x between times y-1 and y. This is simple
to code, (I just put the discrete time points and the 32000 people in
their own vectors, V1 and V2):


for (i in 1:32000){
for (j in 1:60000){

for (k in 2:14){

if (M2[1, j] == V2[i]   &   M2[2, j]  > V1[k-1]   &  M2[2, j] <= V1[k])
M3[i, k] = M3[i, k]+1

}}}

This would work im sure, the problem is that it takes a very long
time. Any advice on helping it run faster would be greatly
appreciated.



More information about the R-help mailing list