[R] If loop

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Fri Aug 9 13:36:10 CEST 2024


On 2024-08-09 6:21 a.m., Bert Gunter wrote:
> "Or use <<- assignment I think.  (I usually return, but return can only
> return one object and I think you want two or more"
> 
> You can return any number of objects by putting them in a list and
> returning the list.
> Use of "<<-" is rarely a good idea in R.

Proper use of "<<-" is often a good idea.  This wouldn't be what I'd 
call proper use.  The example in section 10.7 of the Intro manual is a 
good use.

It would be a positive change to the language if the target of the 
assignment was required to be an existing variable in a parent environment.

Duncan Murdoch


> 
> -- Bert
> 
> On Fri, Aug 9, 2024 at 1:53 AM CALUM POLWART <polc1410 using gmail.com> wrote:
>>
>> OK. The fact it's in a function is making things clearer.
>>
>> Are you trying to update the values of an object from within the function,
>> and have them available outside the function. I don't speak functional
>> programming articulately enough but basically
>>
>> v <- 1
>>
>> funA <- function() {
>> v <- v+1
>> }
>>
>> funA()
>> cat (v)
>>
>> # 1
>>
>> You either return the v from the function so
>>
>> funB <- function() {
>> v <- v+1
>> return (v)
>> }
>>
>> v <- funB()
>> cat (v)
>> #2
>>
>> Or use <<- assignment I think.  (I usually return, but return can only
>> return one object and I think you want two or more
>>
>> v <- 1
>> funC <- function() {
>> v <<- v+1
>> }
>>
>> funC()
>> cat (v)
>> #2
>>
>> On Fri, 9 Aug 2024, 09:03 Steven Yen, <styen using ntu.edu.tw> wrote:
>>
>>> Thanks. Hmm. The loop is doing what it is supposed to do.
>>>
>>>> try1<-function(joint12=FALSE,marg1=FALSE,marg2=FALSE,
>>> +                cond12=FALSE,cond21=FALSE){
>>> + # ***************************************************
>>> + # Testing if loop
>>> + # ***************************************************
>>> + if(joint12){
>>> +   {print ("joint12"); cat(joint12,"\n")}
>>> +   {print ("marg1"); cat(marg1,"\n")}
>>> + } else if (marg1) {
>>> +   {print ("marg1"); cat(marg1,"\n")}
>>> +   {print ("joint12"); cat(joint12)}
>>> + } else if (marg2) {
>>> +   {print ("marg2"); cat(marg2)}
>>> + } else if (cond12) {
>>> +   {print ("cond12"); cat(cond12)}
>>> + } else {
>>> +   {print ("cond21"); cat(cond21)}
>>> + }}
>>>> try1(joint12=TRUE)
>>> [1] "joint12"
>>> TRUE
>>> [1] "marg1"
>>> FALSE
>>>> try1(marg1=TRUE)
>>> [1] "marg1"
>>> TRUE
>>> [1] "joint12"
>>> FALSE
>>>> try1(marg2=TRUE)
>>> [1] "marg2"
>>> TRUE
>>>> try1(cond12=TRUE)
>>> [1] "cond12"
>>> TRUE
>>>> try1(cond21=TRUE)
>>> [1] "cond21"
>>> TRUE
>>>>
>>> On 8/9/2024 2:35 PM, CALUM POLWART wrote:
>>>
>>> Is something wrong in the initialisation part that we don't see?
>>>
>>> joint12 <- marg1 <-F
>>>
>>> marg1 <-T
>>>
>>> if (joint12) {
>>>    print ("joint 12")
>>>    cat (joint12)
>>> }
>>>
>>> if (marg1) {
>>>    print("marg 1")
>>>    cat(marg1)
>>> }
>>>
>>> Would probably be my diagnostic approach
>>>
>>> On Fri, 9 Aug 2024, 04:45 Steven Yen, <styen using ntu.edu.tw> wrote:
>>>
>>>> Can someone help me with the if loop below? In the subroutine, I
>>>> initialize all of (joint12,marg1,marg2,cond12,cond21) as FALSE, and call
>>>> with only one of them being TRUE:
>>>>
>>>> ,...,joint12=FALSE,marg1=FALSE,marg2=FALSE,cond12=FALSE,cond21=FALSE,,,,
>>>>
>>>> joint12 seems to always kick in, even though I call with, e.g., marg1
>>>> being TRUE and everything else being FALSE. My attempts with if... else
>>>> if were not useful. Please help. Thanks.
>>>>
>>>> v1<-cprob(z1,x1,a,b,mu1,mu2,rho,j+1,k+1)
>>>>       v0<-cprob(z0,x0,a,b,mu1,mu2,rho,j+1,k+1)
>>>>
>>>>      ...
>>>>
>>>>       me1<-me0<-NULL
>>>>       if(joint12) {me1<-cbind(me1,v1$p12); me0<-cbind(me0,v0$p12)}
>>>>       if(marg1)   {me1<-cbind(me1,v1$p1); me0<-cbind(me0,v0$p1)}
>>>>       if(marg2)   {me1<-cbind(me1,v1$p2); me0<-cbind(me0,v0$p2)}
>>>>       if(cond12)  {me1<-cbind(me1,v1$pc12); me0<-cbind(me0,v0$pc12)}
>>>>       if(cond21)  {me1<-cbind(me1,v1$pc21); me0<-cbind(me0,v0$pc21)}
>>>>       ...
>>>>
>>>>     labels<-NULL
>>>>     if(joint12) labels<-c(labels,lab.p12)
>>>>     if(marg1)   labels<-c(labels,lab.p1)
>>>>     if(marg2)   labels<-c(labels,lab.p2)
>>>>     if(cond12)  labels<-c(labels,lab.pc12)
>>>>     if(cond21)  labels<-c(labels,lab.pc21)
>>>>
>>>> ______________________________________________
>>>> 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.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list