[R] Odp: R-way to doing this?
Petr PIKAL
petr.pikal at precheza.cz
Thu Dec 23 07:56:53 CET 2010
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 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.
>
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 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