Skip to content
Prev 284494 / 398502 Next

Vectorizing a loop

On Tue, Feb 07, 2012 at 11:39:42AM -0500, Alexander Shenkin wrote:
Hi.

Try the following.

  cond1 <- (df[,3] %in% c(FALSE,NA)) & (df[,1] > 2 | df[,5])
  df[,4] <- ifelse(cond1, 1, df[,4])
  cond2 <- (df[,5] %in% c(TRUE, NA)) & (df[,2] == "b")
  df[,4] <- ifelse(cond2, 2, df[,4])
  df[,5] <- ifelse(cond2, TRUE, df[,5])

Hope this helps.

Petr Savicky.