[R] conditonal "if"
Erik Iverson
eriki at ccbr.umn.edu
Sat Jul 31 01:26:28 CEST 2010
On 07/30/2010 06:04 PM, putri wikie wrote:
> Hi,
>
> I have problem with my R program. Actually, it just a simple program, but I do
> not know the error is. Here is part of my program:
>
> ....
> if (tau2ca==0) {MVvc<- 0.01} else {MVvc<- tau2ca}
> ri_vc<- si2/MVvc
> vi_vc<- ri_vc + 1
> Y_bar_vc<- sum(y/vi_vc)/sum((1/vi_vc))
> tau2MVvc<- sum((1/vi_vc)*(y - Y_bar_vc)^2)/(k-1)
> ....
>
> But then, there is an error displayed in workspace
>
> Error in if (tau2ca == 0) { : missing value where TRUE/FALSE needed
>
> I do not know why there is an error like that.
R is telling you exactly why there is an error. The "if" statement needs to
evaluate to TRUE or FALSE, but it is evaluating to a missing value (e.g., NA).
tau2ca is probably NA at that point.
Since you don't provide a reproducible example as the posting guide asks, I have
no idea why it is missing.
> Also, I just want to make sure whether my program below is right or
> not.
>
> for (l in 1:k)
> {if (a[l]==0 | b[l]==0 | c[l]==0 | d[l]==0)
> { a[l]<- a[l]+0.5
> b[l]<- b[l]+0.5
> c[1]<- c[1]+0.5
> d[1]<- d[1]+0.5
> }
> }
Since this is not self-contained, nor are your objectives stated, there is no
way to tell you wheter or not the program is "correct".
There is surely a vectorized/list approach to what you want to do.
More information about the R-help
mailing list