if statement and for loop question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

if statement and for loop question

R help mailing list-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: if statement and for loop question

Jeff Newmiller
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: if statement and for loop question

Berry, Charles-2
In reply to this post by R help mailing list-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: if statement and for loop question

Rui Barradas
In reply to this post by R help mailing list-2
Hello,

You don't need a loop, the R way is a vectorized solution and it's also
clearer.
Create a logical index (note only one &) and assign b, c, d where it's TRUE.


i <- try$a != "Positive" & try$a != "VUS"
try <- within(try, {
   b[i] <- ''
   c[i] <- ''
   d[i] <- ''
})


Hope this helps,

Rui Barradas

Às 17:28 de 30/05/21, Kai Yang via R-help escreveu:

> 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]== ''
>      try$c[i] == ''
>      try$d[i]== ''
>    }
> }
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: if statement and for loop question

Jim Lemon-4
In reply to this post by R help mailing list-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: if statement and for loop question

R help mailing list-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: if statement and for loop question

Michael Dewey-3
CONTENTS DELETED
The author has deleted this message.