vendredi 1 octobre 2021

Skip specific observations when using row_number() - R

I'm essentially after a "next" statement which I can use within a dplyr ifelse statement, although other R alternatives are also welcome.

Here's the code so far:

df1 <- data%>%
  arrange(Var1, Var2, Var3, Var4, Var5)%>%
  group_by(Var1)%>%
  distinct(Var1, Var2, Var3, Var4, Var5)%>%
  mutate(Var6 = ifelse(Var4 == "COMPLETE", row_number(), row_number()+1))

the output is (relevant version)

  | Var4         | Var6         |
  | ------------ | -------------|
  | COMPLETE     | 1            |
**| INCOMPLETE   | 3            |**
  | COMPLETE     | 3            |
  | COMPLETE     | 4            |
  | COMPLETE     | 5            |
**| INCOMPLETE   | 7            |**
  | COMPLETE     | 7            |
  | COMPLETE     | 8            |
  | COMPLETE     | 9            |


the intended output is


  | Var4         | Var6         |
  | ------------ | -------------|
  | COMPLETE     | 1            |
**| INCOMPLETE   | 2            |**
  | COMPLETE     | 2            |
  | COMPLETE     | 3            |
  | COMPLETE     | 4            |
**| INCOMPLETE   | 5            |**
  | COMPLETE     | 5            |
  | COMPLETE     | 6            |
  | COMPLETE     | 7            |

In summary, my goal is that when Var4 == INCOMPLETE I am able to ignore that row and continue with row_number().

Aucun commentaire:

Enregistrer un commentaire