Just tried your suggestion. rawdata[is.na(rawdata), ] <- 0 It FAILS with the following error: Error in `[<-.data.frame`(`*tmp*`, is.na(rawdata), , value = 0) : non-existent rows not allowed