Re: [R] If Loop With Lagged Variable
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(ari18.test3$Date) != 0) ari18.test3$GameNum <- ari18.test3$GameNum + cumsum(d) ari18.test3 # Date GameNum #1 2018-03-29 1 #2 2018-03-29 1 #3 2018-03-29 1 #4 2018-03-29 1 #5 2018-03-30 2 #6 2018-03-30 2 #7 2018-03-30 2 #8 2018-03-30 2 #9 2018-04-01 3 #10 2018-04-01 3 #11 2018-04-01 3 #12 2018-04-01 3 Hope this helps, Rui Barradas Às 22:09 de 19/09/19, Rui Barradas escreveu: 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 tested, since there is no data.) Hope this helps, Rui Barradas Às 17:27 de 19/09/19, Phillip Heinrich escreveu: 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 variables: $ GameID : Factor w/ 3 levels "ARI201803290",..: 1 1 1 1 1 1 1 1 1 1 ... $ Date : Date, format: "2018-03-29" "2018-03-29" "2018-03-29" "2018-03-29" ... $ DHCode : Factor w/ 1 level "0": 1 1 1 1 1 1 1 1 1 1 ... $ GameNum: num 1 1 1 1 1 1 1 1 1 1 ... $ Date2 : Date, format: NA "2018-03-29" "2018-03-29" "2018-03-29" ... I’m trying to increment the GameNum (game number) to game 2 when the date changes from 2018-03-29 to 2018-03-30 in row 81 and to game 3 in row 165. According to my R for Dummies book the following code should work but it doesn’t. I keep getting the following error. Any suggestions? if(ari18.test3$Date > lag(ari18.test3$Date)) {ari18.test3$gameNum <- ari18.tesm3$GameNum + 1} Warning message: In if (ari18.test3$Date > lag(ari18.test3$Date)) { : the condition has length > 1 and only the first element will be used > Thanks. __ R-help@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@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@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.
Re: [R] If Loop With Lagged Variable
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 tested, since there is no data.) Hope this helps, Rui Barradas Às 17:27 de 19/09/19, Phillip Heinrich escreveu: 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 variables: $ GameID : Factor w/ 3 levels "ARI201803290",..: 1 1 1 1 1 1 1 1 1 1 ... $ Date : Date, format: "2018-03-29" "2018-03-29" "2018-03-29" "2018-03-29" ... $ DHCode : Factor w/ 1 level "0": 1 1 1 1 1 1 1 1 1 1 ... $ GameNum: num 1 1 1 1 1 1 1 1 1 1 ... $ Date2 : Date, format: NA "2018-03-29" "2018-03-29" "2018-03-29" ... I’m trying to increment the GameNum (game number) to game 2 when the date changes from 2018-03-29 to 2018-03-30 in row 81 and to game 3 in row 165. According to my R for Dummies book the following code should work but it doesn’t. I keep getting the following error. Any suggestions? if(ari18.test3$Date > lag(ari18.test3$Date)) {ari18.test3$gameNum <- ari18.tesm3$GameNum + 1} Warning message: In if (ari18.test3$Date > lag(ari18.test3$Date)) { : the condition has length > 1 and only the first element will be used > Thanks. __ R-help@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@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] If Loop With Lagged Variable
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 variables: $ GameID : Factor w/ 3 levels "ARI201803290",..: 1 1 1 1 1 1 1 1 1 1 ... $ Date : Date, format: "2018-03-29" "2018-03-29" "2018-03-29" "2018-03-29" ... $ DHCode : Factor w/ 1 level "0": 1 1 1 1 1 1 1 1 1 1 ... $ GameNum: num 1 1 1 1 1 1 1 1 1 1 ... $ Date2 : Date, format: NA "2018-03-29" "2018-03-29" "2018-03-29" ... I’m trying to increment the GameNum (game number) to game 2 when the date changes from 2018-03-29 to 2018-03-30 in row 81 and to game 3 in row 165. According to my R for Dummies book the following code should work but it doesn’t. I keep getting the following error. Any suggestions? if(ari18.test3$Date > lag(ari18.test3$Date)) {ari18.test3$gameNum <- ari18.tesm3$GameNum + 1} Warning message: In if (ari18.test3$Date > lag(ari18.test3$Date)) { : the condition has length > 1 and only the first element will be used > Thanks. __ R-help@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.