performance - Very slow R loop -
i trying populate column follows:
at t-1
: if nav(t-1) - hwm(t-1) >0
then @ t
: hwm(t) = nav(t)
otherwise: hwm(t) = hwm(t-1)
my r skills quite elementary , following loop, though works, excruciatingly slow (150,000 observations)
for (i in 1:length(e$hwm)) { print(i) hwm[e$t>2][i] <- ifelse( nav_lag1[e$t>2][i] - hwm_lag1[e$t>2][i] > 0 , nav_lag1[e$t>2][i] , hwm_lag1[t>2[i] ) hwm_lag1[i] <-lag(hwm,1)[i] }
any suggestions make process more efficient?
thank much.
here have:
zz <- "t nav hwm nav_lag1 hwm_lag1 1 1000 na na na 2 1200 1000 1000 na 3 1400 na 1200 na 4 1200 na 1400 na 5 1100 na 1200 na 5 1000 na 1100 na "
here get:
zz <- "t nav hwm nav_lag1 hwm_lag1 1 1000 na na na 2 1200 1000 1000 1000 3 1400 1200 1200 1000 4 1200 1400 1400 1200 5 1100 1400 1200 1400 5 1000 1400 1100 1400" data <- read.table(text=zz, header = true)