[R] If Loop With Lagged Variable
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Sep 19 23:09:32 CEST 2019
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 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