mardi 1 mai 2018

Managing NA values in data frames while running if else loop

I have two data frames where I want to run this code. Both data frames are of the same size. psd.csv has some values that are blanks.

The objective is that I want NA to remain as NA in the data frame nmsd too.

This code is showing the following error :

Error in

if (psd[i, j] == "NA") nmsd[i, j] = psd[i, j] else if (psd[i, : missing value where TRUE/FALSE needed

   for(j in 1:500){
     for(i in 1:3418){
       if(psd[i,j]=="NA")
         nmsd[i,j] = psd[i,j]
       else if(psd[i,j]>=0 && psd[i,j]<=0.3)
         nmsd[i,j]=psd[i,j]+0.3*(1-psd[i,j])
       else if(psd[i,j]>0.3 && psd[i,j]<=0.4) 
         nmsd[i,j]=psd[i,j]+0.1*(1-psd[i,j])
       else if(psd[i,j]>0.4 && psd[i,j]<=0.6) 
         nmsd[i,j]=psd[i,j]+0.2*(1-psd[i,j])
       else if(psd[i,j]>0.6 && psd[i,j]<=0.9) 
         nmsd[i,j]=psd[i,j]+0.3*(1-psd[i,j])
       else(psd[i,j]>0.9 && psd[i,j]<=1) 
         nmsd[i,j]=psd[i,j]+0.1*(1-psd[i,j])
          }
         }

Aucun commentaire:

Enregistrer un commentaire