I'm trying to translate a SAS code containing "retain" statement in R {dplyr}
data verif;
length COMMENTAIRE $20.;
retain COMMENTAIRE ;
set verif1;
if top= 1 then COMMENTAIRE = "delete";
if sample not in ("F","Z") then COMMENTAIRE = "OK";
run;
Is there a way to keep values and change them if a condition is encountered in R like the "retain" statement does it in SAS?
What I'd like to have :
top | sample | commentaire |
---|---|---|
0 | F | ok |
1 | F | delete |
0 | F | delete |
0 | E | ok |
0 | F | ok |
1 | F | delete |
0 | F | delete |
To explain it more :
I'd like to keep a value until a new condition is encountered. So in the example above, it means that :
If top=1, then values for each row will be "delete" until the condition 'sample not in ("F","Z")' is not encountered. If this condition is encountered, the new value for the following rows will be "OK" until the condition "top=1" is encountered, and then the values for following rows will be "delete" etc.
Hoping it will be easier to understand
Thank you very much