R - Assigning "NA" to objects 'not found' inside a function; is it possible? -


i running data set (in example, "data object ") through several different functions in r , concatenating numeric results @ end. see:

a<-median((function1(x=1,dataobject,reps=500)),na.rm=true) b<-median((function2(x=1,dataobject,reps=500)),na.rm=true) c<-median((function3(x=1,dataobject,reps=500)),na.rm=true)  d<-median((function4(x=1,dataobject,reps=500)),na.rm=true) e<-median((function5(x=1,dataobject,reps=500)),na.rm=true) f<-median((function6(x=1,dataobject,reps=500)),na.rm=true)  c(a,b,c,d,e,f) 

however, of functions cannot run data set using, , return error; e.g. "function3" can't run when gets concatenation step gives "error: object 'e' not found" , not return anything. there way tell r @ concatenation step assign value of "na" object not found , continue run rest of code instead of stopping? return be

[1] 99.233 75.435 77.782 92.013 na 97.558  

a simple question, not find other instances of being asked. tried set function run , output concatenated results, ran same problem (when function can't run, entire wrapper function stops , don't know how tell r skip can't compute).

any thoughts appreciated! thanks!

a couple of solutions can think of,

  1. initialize variables plan use, have default value want.

    a = b = c = d = e = na

then run code. if error pops up, have na in variable.

  1. use "trycatch". if unaware is, recommend reading on it. lets handle errors.

here example code,

trycatch({   a<-median((function1(x=1,dataobject,reps=500)),na.rm=true) }, error = function(err){   print("error in evaluating a. initializing na")   <<- na }) 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -