Skip to content
Prev 318596 / 398503 Next

replace zeros for NA in a column based on values of another column

Hello,

Arun has a point, I forgot to check the element to be replaced for a 
zero. The following code will do that.

dat <- read.table(text = "
a       b       c       d
0       1       1       0
1       1       1       1
1       0       0       1
", header = TRUE)
dat <- data.matrix(dat)
str(dat)

dat[, 1:2] <- sapply(1:2, function(i) ifelse(dat[, i] == 0 & dat[, 4] == 
0, NA, dat[, i]))


Hope this helps,

Rui Barradas

Em 02-03-2013 16:11, arun escreveu: