[R] Can I replace NA by 0 (if yes, how) ?

Julian Burgos jmburgos at u.washington.edu
Thu Nov 8 01:16:26 CET 2007


Do this:

pfit$coefficients[is.na(pfit$coefficients)]=0

Julian

Ptit_Bleu wrote:
> Hello,
> 
> I'm trying to fit some points with a 8-degrees polynom (result of lm is
> stored in pfit).
> In most of the case, it is ok but for some others, some coefficients are
> "NA".
> I don't really understand the meaning of these "NA".
> 
> And the problem is that I can't perform a derivation
> (pderiv<-as.function((deriv(polynomial(pfit$coefficients))))) on pfit due to
> the presence of these "NA".
> 
> I tried the functions na.omit and na.exclude but "NA" are still there.
> I tried to replace manually "NA" by 0. The fit seems ok and then I can
> derive the polynom.
> 
> But can I do this and, if yes, how can I detect and automatically replace
> these "NA" ?
> 
> To conclude, I must say that I read some posts on NA but I must confess that
> it is still not clear to me (maybe because I'm french and R-newbie ...)
> 
> Thanks in advance for your help,
> Ptit Bleu.
> 
> 
> pfit
> 
> Call:
> lm(formula = pfitmax ~ poly(vfitmax, 8, raw = T))
> 
> Coefficients:
>                (Intercept)  poly(vfitmax, 8, raw = T)1  poly(vfitmax, 8, raw
> = T)2  poly(vfitmax, 8, raw = T)3  poly(vfitmax, 8, raw = T)4  
>                 -2.790e+04                   6.276e+03                 
> -5.645e+02                   2.591e+01                  -6.241e-01  
> poly(vfitmax, 8, raw = T)5  poly(vfitmax, 8, raw = T)6  poly(vfitmax, 8, raw
> = T)7  poly(vfitmax, 8, raw = T)8  
>                  6.681e-03                          NA                 
> -3.942e-07                          NA  
> 
> 
>> pderiv<-as.function((deriv(polynomial(pfit$coefficients))))
> Erreur dans while ((la <- length(a)) > 1 && a[la] == 0) a <- a[-la] : 
>         valeur manquante là où TRUE / FALSE est requis
>



More information about the R-help mailing list