Replace all values in a row with 0 if one of the variables has missing data

南笙酒味 提交于 2019-12-02 03:55:29

complete.cases seems appropriate here:

dat[!complete.cases(dat),] <- 0
dat
#  x y z
#1 2 3 5
#2 0 0 0
#3 3 2 1

While the answer by @thelatemail is really cool,

It won't hurt to learn one more trick.

Here is my solution:

dat[rowSums(is.na(dat))>0,]<- 0
dat
#  v1 v2 v3
#1 2  3  5
#2 0  0  0
#3 3  2  1
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!