Skip to content

Instantly share code, notes, and snippets.

@b-rodrigues
Last active April 10, 2017 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b-rodrigues/a119949bcb94bcafc0f4eecdf95b914a to your computer and use it in GitHub Desktop.
Save b-rodrigues/a119949bcb94bcafc0f4eecdf95b914a to your computer and use it in GitHub Desktop.
Create a "counter" variable that gets incremented by one for each occurence of interest
library(dplyr)
data("iris")
# At the first occurence of, say, Petal.Length = 1 and Petal.Width = 1, counter = 1. If Petal.Length = 1 and Petal.Width = 1 occur
# again, counter gets incremented to 2
iris %>%
group_by(Petal.Length, Petal.Width) %>%
mutate(counter = seq_along(Species))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment