Skip to content
Prev 176942 / 398503 Next

Faster Solution for a simple code?

Hi R-users,

I create a simple code to check out how often the same numbers in y occur in
x. For example 5000000 320000 occurs two times. 
But the code with the loop is extremly slow. x have 6100 lines and y
sometimes more than 50000 lines.

Is there any alternative code to create with R?

thanks.
 

x

5000000	3200000	0
5100000	3100000	0
5200000	3100000	0
5200000	3200000	0


lengthx <- length(x[,1])

y

5000000	3200000	1
5000000	3200000	1
5200000	3100000	1
5200000	3000000	1


langthy <- length(y[,1])

for (i in 1:lengthx){
for (j in 1:lengthy){
if (x[i,1] == y[j,1]){
if (x[i,2] == y[j,2]){
x[i,3] <- x[i,3] + 1
}
}
}
}
x

1  5000000    3200000      2
2  5100000    3100000      0
3  5200000    3100000      1
4  5200000    3200000      0