[R] Odp: Substitute "Object not found" with NA

Petr PIKAL petr.pikal at precheza.cz
Fri Dec 4 13:06:52 CET 2009


Hi


r-help-bounces at r-project.org napsal dne 04.12.2009 12:09:08:

> Hi everybody,
> I'm trying to create a script that picks a txt file with 2 columns of 
> coordinates (depth and variable1) and automatically tries to fit several 

> polynomial with the function nls().
> After  that, it creates a list of observed, predict, residuals and 
other, and 
> then it calculates AIC, RMSD, MAD and R^2.
> At the end of the script I create a series of vectors with MAD for all 
> polynomials, RMSD for all polynomial and so on, and from a matrix a .csv 
file.
> This script is completely automatic, but there is a problem if one or 
more 
> models don't fit, because if one or more data is missing I cannot create 
the 
> vectors and the matrix, as well.
> 
> So, my question is: is it possible to create a vector with a missing 
object, 
> substituting that object with NA in the vector?
> 
> For example:
> I want to create this vector
> Vec1<-c(MAD1,MAD2,MAD3,MAD4)
> but MAD4 is missing and as a consequence R return an error like this:
> "object 'MAD4' not found"

Look at ?try function.

I did something similar

#this is just a selection if inputed start values shall be used

fit<-try(nls(formula[[i]], data, ...))
} else {
fit<-try(nls(formula[[i]], data, start, ...)) }
}

#here is a test whether fit had some result, if yes values are extracted 
from fit if not, NA is put into list

if( class(fit)=="try-error") result1[i] <- NA  else result1[i] <- 
sum(resid(fit)^2, na.rm=T)
if( class(fit)=="try-error") result2[[i]] <- NA  else result2[[i]] <- 
coef(fit)
}

Regards
Petr


> 
> Is it possible to say to R that if it does not find an object it must 
> substitute that object with NA in the vector?
> 
> Many Thanks, Fabio
> 
> 
> Mr. Fabio Veronesi
> Ph.D. student
> 
> Cranfield University, Bld.37
> School of Applied Sciences
> MK43 0AL
> Cranfield, Bedfordshire
> 
> Tel. +44 (0)7984049316
> e-mail: f.veronesi at cranfield.ac.uk
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list