I want to replace all 0 values to NA but only in subset of columns.
df <- data.frame(replicate(100,sample(0:9,1000,rep=TRUE)))
To change all 0 to NA in the entire df one should use
df[df == 0] <- NA
but I want to change 0 to NA only in subset of columns 5:100, I have tried
df[df == 0][ , 5:100] <- NA
df[df == 0][5:100] <- NA
df[ , 5:100][df == 0] <- NA
df[5:100][df == 0] <- NA
df[5:100][df[5:1000] == 0] <- NA
df[df[5:1000] == 0] <- NA
df[which(df[, 5:100] == 0)] <- NA
df[which(df[5:100] == 0)] <- NA
but all of that returns an error.
How to apply the operation to part of the df?