conditonal "if"

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

conditonal "if"

putri wikie
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. I think I type the program
correctly. 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
}
}

Looking forward to hearing from you.


Many thanks,
-pwikie-



     
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: conditonal "if"

Wu Gong
I replicated your error.

tau2ca <- NA
if (tau2ca==0) {x <- 0.01} else {x <- tau2ca}

Hope it helps. And I couldn't replicate your second program, but it seems ok.
Reply | Threaded
Open this post in threaded view
|

Re: conditonal "if"

Erik Iverson-3
In reply to this post by putri wikie
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.

______________________________________________
[hidden email] 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.