Dear friends, hope I could be able to explain my problem through following example. Please consider this:> set.seed(1)> input <- rnorm(10)> input[1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 -0.8204684 0.4874291 0.7383247 0.5757814 -0.3053884> tag <- vector(length=10)for(i in 1:10) # if there is any ****error**** in evaluating "log(input[i])" (or evaluating some function) then tag[i] = 1, otherwise tag[i] = 0 Therefore my "tag" vector should like: tag[1]=1, tag[2]=0, tag[3]=1, tag[4]=0, tag[5]=0....... Actually R returns logarithm of a negative number as NaN, however in this example please consider R returns error when it encounters logarithm of a negative number. Is there any way to do above task? Thanks and regards, [[alternative HTML version deleted]]
Hi r-help-bounces at r-project.org napsal dne 23.12.2010 05:13:37:> Dear friends, hope I could be able to explain my problem throughfollowing> example. Please consider this: > > > > > set.seed(1) > > > input <- rnorm(10) > > > input > > [1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 -0.8204684 > 0.4874291 0.7383247 0.5757814 -0.3053884 > > > tag <- vector(length=10) > > > > for(i in 1:10) > > # if there is any ****error**** in evaluating "log(input[i])" (or > evaluating some function) then tag[i] = 1, otherwise tag[i] = 0 > > > > Therefore my "tag" vector should like: tag[1]=1, tag[2]=0, tag[3]=1, > tag[4]=0, tag[5]=0....... > > > > Actually R returns logarithm of a negative number as NaN, however inthis> example please consider R returns error when it encounters logarithm ofa> negative number. >I do not understand above sentence well. R returns NaN not error. For error handling in loop use try or tryCatch. For simple vectorised function evaluation as log you can use is.* evaluation is.nan(log(input))*1 [1] 1 0 1 0 0 1 0 0 0 1 ifelse(is.nan(log(input)), 1,0) [1] 1 0 1 0 0 1 0 0 0 1 Regards Petr> > > Is there any way to do above task? > > > > Thanks and regards, > > > [[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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
If I understand your question properly, then you are looking for 'try' or 'tryCatch'. There is an example of using these on page 89 of 'The R Inferno'. On 23/12/2010 04:13, Bogaso Christofer wrote:> Dear friends, hope I could be able to explain my problem through following > example. Please consider this: > > > >> set.seed(1) > >> input<- rnorm(10) > >> input > > [1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 -0.8204684 > 0.4874291 0.7383247 0.5757814 -0.3053884 > >> tag<- vector(length=10) > > > > for(i in 1:10) > > # if there is any ****error**** in evaluating "log(input[i])" (or > evaluating some function) then tag[i] = 1, otherwise tag[i] = 0 > > > > Therefore my "tag" vector should like: tag[1]=1, tag[2]=0, tag[3]=1, > tag[4]=0, tag[5]=0....... > > > > Actually R returns logarithm of a negative number as NaN, however in this > example please consider R returns error when it encounters logarithm of a > negative number. > > > > Is there any way to do above task? > > > > Thanks and regards, > > > [[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. >-- Patrick Burns pburns at pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')