I have a database with thousands of records. The "for i loop" iteration takes a long time to execute, it is not possible to use it because of the size of the database.
I need to perform an iteration where:
- if row i of x is equal to row i-1 of x then,
- y(i) = y(i-1) + z(i), y is a cumulative variable
- Else y(i) = z(i)
Previously, the database is sorted by variable x
for (i in 2:n) {
if (db$x[i] == db$x[i - 1]) {
db$y[i] <- db$y[i - 1] + db$z[i]
} else {
db$y[i] <- db$z[i]
}
}
Is there a way to make it more efficient?
Any help will be appreciated