I am trying to create a calculated field in R using an if statement that compares strings, which in Excel is easy but I can't figure it out in R. The formula in Excel looks as follows:
=IF(AND(E2<>"Accepted", B2=B3,E3="Accepted" ), -(F2-F3),"")
Where Column B is a list of 9-digit numbers (i.e., 200100111), and Column F contains dates. The code I wrote in R is as follows:
if(df_srt$NUMBER==shift(df_srt$NUMBER, 1L, type="lead") && df_srt$DESCRIPTION != "Accepted" && shift(VA_df_srt$DESCRIPTION, 1L, type="lead")=="Accepted") {1} else {0}
I then realized that the if statement only compares vector length of 1 so that did not work. I can individually compare strings to get a "TRUE" value as follows:
TEST2calc <- df_srt$NUMBER==shift(df_srt$NUMBER, 1L, type="lead")
But I want to have those three comparisons and then assign a value if they are all true.
Thanks
Aucun commentaire:
Enregistrer un commentaire