Re: [R] If loop

2024-08-10 Thread martin gregory via R-help
> On 9. Aug 2024, at 10:45, CALUM POLWART wrote: > > Or use <<- assignment I think. (I usually return, but return can only > return one object and I think you want two or more > One can return multiple objects by putting them in a list and returning the list. Martin __

Re: [R] If loop

2024-08-09 Thread CALUM POLWART
Oh I thought that was just my experience of it !! So didn't want to slate it too much!! Yes, I nearly said "without making it an object of objects". I guess it depends what they want to do with those objects in the end. Usually if I want a function to manipulate two things I create two functions.

Re: [R] If loop

2024-08-09 Thread Duncan Murdoch
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.

Re: [R] If loop

2024-08-09 Thread Steven Yen
Thanks. Hmm, I like the idea of returning a list. I occasionally use “invisible” and am still trying to figure out its difference with return. Where do I read about <<- assignment? On 8/9/2024 6:21 PM, Bert Gunter wrote: > "Or use <<- assignment I think. (I usually return, but return can only >

Re: [R] If loop

2024-08-09 Thread Bert Gunter
"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. -- Bert On Fri, Aug 9, 2024 at 1:53 AM CALUM

Re: [R] If loop

2024-08-09 Thread CALUM POLWART
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()

Re: [R] If loop

2024-08-09 Thread Steven Yen
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(j

Re: [R] If loop

2024-08-08 Thread CALUM POLWART
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, wrote: > Can som

Re: [R] If loop

2024-08-08 Thread Rui Barradas
Às 05:33 de 09/08/2024, Steven Yen escreveu: The following (using if else) did not help. Seemed like joint12 always kicked in.     me1<-me0<-NULL.     if(joint12){   {me1<-cbind(me1,v1$p12);  me0<-cbind(me0,v0$p12)}     } else if(marg1) {   {me1<-cbind(me1,v1$p1);   me0<-cbind(me0,

Re: [R] If loop

2024-08-08 Thread Bert Gunter
1. I think to get useful help you will have to provide a small reproducible example. In any case, I was not able to figure out your problem from what you provided (perhaps others may). 2. R is a (mostly) functional language. It does not have "subroutines" . Cheers, Bert On Thu, Aug 8, 2024 at 8:

Re: [R] If loop

2024-08-08 Thread Steven Yen
The following (using if else) did not help. Seemed like joint12 always kicked in.     me1<-me0<-NULL.     if(joint12){   {me1<-cbind(me1,v1$p12);  me0<-cbind(me0,v0$p12)}     } else if(marg1) {   {me1<-cbind(me1,v1$p1);   me0<-cbind(me0,v0$p1)}     } else if(marg2) {   {me1<-cbind(me

[R] If loop

2024-08-08 Thread Steven Yen
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 ca

Re: [R] If Loop I Think

2019-10-27 Thread William Michels via R-help
Hi Phillip, I wanted to follow up with you regarding your earlier post. Below is a different way to work up your data than I posted earlier. I took the baseball data you posted, stripped out leading-and-following blank lines, removed all trailing spaces on each line, and removed the "R1", "R2" an

Re: [R] If Loop I Think

2019-10-24 Thread William Michels via R-help
W. Michels, Ph.D. On Wed, Oct 23, 2019 at 12:40 AM PIKAL Petr wrote: > > Hi > > ***do not think in if or if loops in R***. > > to elaborate Jim's solution further > > With simple function based on logical expression > fff <- function(x) (x!="")+0 >

Re: [R] If Loop I Think

2019-10-23 Thread PIKAL Petr
Behalf Of Jim Lemon > Sent: Wednesday, October 23, 2019 12:26 AM > To: Phillip Heinrich > Cc: r-help > Subject: Re: [R] If Loop I Think > > Hi Philip, > Try this: > > phdf<-read.table( > text="Row Outs RunnerFirst RunnerSecond RunnerThird R1 R2 R3 > 1 0

Re: [R] If Loop I Think

2019-10-22 Thread Jim Lemon
x27;ll know more once I'm done. > > Can you suggest any other sources? > > Thanks. > > -Original Message- > From: Jim Lemon > Sent: Tuesday, October 22, 2019 3:26 PM > To: Phillip Heinrich > Cc: r-help > Subject: Re: [R] If Loop I Think > > Hi Philip, >

Re: [R] If Loop I Think

2019-10-22 Thread David Winsemius
On 10/22/19 1:54 PM, Phillip Heinrich wrote: Row Outs RunnerFirst RunnerSecond RunnerThird R1 R2 R3 1 0 2 1 3 1 4 1 arenn001 5 2 arenn001 6 0 7 0 perad001 8 0 polla001 perad001 9 0 goldp001 polla001 perad001 10 0 lamb

Re: [R] If Loop I Think

2019-10-22 Thread Jim Lemon
Hi Philip, Try this: phdf<-read.table( text="Row Outs RunnerFirst RunnerSecond RunnerThird R1 R2 R3 1 0 2 1 3 1 4 1 arenn001 5 2 arenn001 6 0 7 0 perad001 8 0 polla001 perad001 9 0 goldp001 polla001 perad001 10 0 lambj001 goldp001 11 1 lambj001 goldp001 12 2 lambj001 13 0 14 1 ", header=

[R] If Loop I Think

2019-10-22 Thread Phillip Heinrich
Row Outs RunnerFirst RunnerSecond RunnerThird R1 R2 R3 1 0 2 1 3 1 4 1 arenn001 5 2 arenn001 6 0 7 0 perad001 8 0 polla001 perad001 9 0 goldp001 polla001 perad001 10 0 lambj001

Re: [R] If Loop With Lagged Variable

2019-09-19 Thread Rui Barradas
Hello, The following might be a better solution. I include a minimal data set as an example. Date <- c(rep(as.Date("2018-03-29"), 4), rep(as.Date("2018-03-30"), 4), rep(as.Date("2018-04-01"), 4)) ari18.test3 <- data.frame(Date) ari18.test3$GameNum <- 1 #--- d <- c(0, diff

Re: [R] If Loop With Lagged Variable

2019-09-19 Thread Rui Barradas
Hello, There was no attachment, R-Help allows only a limited number of file types, see the posting guide and try reposting. As for the question, try ifelse, the vectorized fom of if/else. ifelse(ari18.test3$Date > lag(ari18.test3$Date), ari18.tesm3$GameNum + 1, ari18.test3$gameNum) (Not t

[R] If Loop With Lagged Variable

2019-09-19 Thread Phillip Heinrich
Attached is every at bat for the Arizona Diamondback’s first three games of 2018 – BBdata1.rda. I added the Date and DHCode variables by parsing the first variable labeled GameID. BBdata2 is a reduced dataset with five variables as shown in the str() command. data.frame':234 obs. of 5 var