[R] if statement and for loop question

Michael Dewey ||@t@ @end|ng |rom dewey@myzen@co@uk
Tue Jun 1 10:51:55 CEST 2021


Dear Kai

When you ask again it is best to tell us what your input is and what 
output you were hoping for and what you actually got. If you can make a 
small data-set which shows all that then your post will be much more 
likely to get a helpful response. If you want to transfer the data-set 
to us then using dput() will ensure that we have exactly the same as you 
have. Do not forget to tell us what libraries, if any, you have loaded too.

Michael

On 31/05/2021 17:26, Kai Yang via R-help wrote:
>    Hi Jim,
> Sorry to post "same" question, because
> 1. I was asking to use plain text format. I have to post my question again. But I don't know if it is working.
> 2. I'm a beginner for R (< 2 month). It may not easy for me to ask a "clear" R question. My current work is to transfer my SAS code into R, especially for data manipulation part.
> I'll do my best to ask a non."same" question later.
> Thanks,
> Kai
>      On Sunday, May 30, 2021, 10:44:41 PM PDT, Jim Lemon <drjimlemon using gmail.com> wrote:
>   
>   Hi Kai,
> You seem to be asking the same question again and again. This does not
> give us the warm feeling that you know what you want.
> 
> testdf<-data.frame(a=c("Negative","Positive","Neutral","Random","VUS"),
>   b=c("No","Yes","No","Maybe","Yes"),
>   c=c("Off","On","Off","Off","On"),
>   d=c("Bad","Good","Bad","Bad","Good"),
>   stringsAsFactors=FALSE)
> testdf
> match_strings<-c("Positive","VUS")
> testdf$b<-ifelse(testdf$a %in% match_strings,testdf$b,"")
> testdf$c<-ifelse(testdf$a %in% match_strings,testdf$c,"")
> testdf$d<-ifelse(testdf$a %in% match_strings,testdf$d,"")
> testdf
> 
> I have assumed that you mean "zero length strings" rather than
> "zeros". Also note that your initial code was producing logical values
> that were never assigned to anything.
> 
> Jim
> 
> On Mon, May 31, 2021 at 2:29 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]== ''
>>      try$c[i] == ''
>>      try$d[i]== ''
>>    }
>> }
>>
>>
>>          [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using r-project.org 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.
>    
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org 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.
> 

-- 
Michael
http://www.dewey.myzen.co.uk/home.html



More information about the R-help mailing list