add column that shows how many times a value has already been repeated from that line back

3

I'm having trouble creating a column that shows how many times a row has already been repeated, but not in the entire dataset, but in that row.

Ex:

NOME     ANO       MES    COLUNA_QUE_QUERO_CRIAR
A        2016      4          1
A        2016      4          2
B        2016      5          1
B        2016      5          2
B        2016      5          3

What I was able to do is that in this column is the value of the entire dataset. Type, pro A gets 2 and 2, pro B 3 3 and 3, but I want that count.

What I did that gave this result that I did not wish was

dataset %>% group_by(NOME) %>% mutate('COLUNA_QUE_QUERO_CRIAR' = n())

Can anyone help me?

    
asked by anonymous 24.11.2017 / 17:23

1 answer

7

Only use the seq function

library(dplyr)
dataset %>% 
  group_by(NOME) %>% 
  mutate(COLUNA_QUE_QUERO_CRIAR = seq(1:n()))
    
24.11.2017 / 17:26