[R] if statement and for loop question

Berry, Charles ccberry @end|ng |rom he@|th@uc@d@edu
Sun May 30 20:08:18 CEST 2021


Kai,

You have made a simple mistake. And now you cannot see it.  I believe this is not uncommon among programmers.  It has happened to me more times than I want to recall.

> On May 30, 2021, at 9:28 AM, Kai Yang via R-help <r-help using r-project.org> wrote:
> 
> Hello List,I have a data frame which having the character columns:
> 
> | a1 | b1 | c1 | d1 |
> | a2 | b2 | c2 | d2 |
> | a3 | b3 | c3 | d3 |
> | a4 | b4 | c4 | d4 |
> | a5 | b5 | c5 | d5 |
> 
> 
> 
> I need to do: if a1 not = "Positive" and not = "VUS" then values of  b1, c1 and d1 will be zero out. And do the same thing for the a2 to a5 series.
> I write the code below to do this. But it doesn't work. Would you please correct my code?
> Thank you,
> Kai
> 
> 
> for (i in 1:5) 
> {
>   if (isTRUE(try$a[i] != "Positive" && try$a[i] != "VUS"))
>   {
>     try$b[i]== ''

What is the above line intended to do?

I think you need to read the line out loud stating the effect of each operator.

If you still do not see what gives, copy and paste each line to the R prompt after the loop has run (i.e. with `i' having value 5) paying attention to any results that are printed out.

>     try$c[i] == ''
>     try$d[i]== ''
>   }
> }
> 
> 
> 	[[alternative HTML version deleted]]
> 

HTH,

Chuck

p.s. It is highly recommended to use `<-' as the assignment operator.



More information about the R-help mailing list