samedi 14 décembre 2019

Fill missing with 0 if all values are 0 with R

I'm trying to fill missing value with R.

If all other value is 0, then I want to fill missing with 0.

An example is shown below. In this data, All value in c column except NA is 0. So, I want to fill Na with 0.

set.seed(1000)
a<-rnorm(10)
b<-rnorm(10)
c<-rep(0,10)
c[c(2,4,8)]<-NA
test<-cbind(a,b,c)

               a          b  c
 [1,]  0.1901328  0.6141360  0
 [2,] -0.9884426  0.6508993 NA
 [3,] -0.9783197  2.1059862  0
 [4,] -1.8584651  0.4354903 NA
 [5,]  0.6623067  1.6382126  0
 [6,] -1.2542872  0.1370791  0
 [7,] -1.9971880  1.9302738  0
 [8,]  1.9417941  0.0449239 NA
 [9,]  1.7046508  1.0726263  0
[10,] -0.7289351 -2.8374912  0

I can't find a good example of code. Would you give me a good advice?

Aucun commentaire:

Enregistrer un commentaire