Skip to content
Prev 359477 / 398502 Next

how to use vectorization instead of for loop

Hello,

I've renamed your dataframe to 'dat'. Since ?ifelse is vectorized, try

dat[, 4] <- ifelse(dat[, 2] > 0, 1 * (1/dat[,3]), -1* (1/dat[,3]))

Oh, and why do you multiply by 1 and by -1?
It would simply be 1/dat[,3] and -1/dat[,3].

Hope this helps,

Rui Barradas


Quoting Stephen HK WONG <honkit at stanford.edu>: