0

I want to create the column Counter

Name Counter
John 0
John 1
John 1
Kate 0
Kate 1
Kate 2
Kate 3

If I used SAS I would write:

if FIRST.Smoking_Status 
  Count = 0;              
Count + 1;                

run;

If every name is supposed to be a group, I have 3000 groups.

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
KLB
  • 1
  • Is line 3 intended to be 2? – Jon Spring Oct 20 '21 at 18:25
  • 1
    `library(dplyr); your_table %>% group_by(Name) %>% mutate(Counter = row_number() - 1)` – Jon Spring Oct 20 '21 at 18:26
  • Thank you for your answer! Yes line 3 intended to be 2. Though your answer seems to work according the comments in Console, the column "counter" is not created in the file – KLB Oct 20 '21 at 18:53
  • Assign that chain to an object to save it. To update the existing table, assign the output to the source table, i.e. `your_table <- your_table %>% group_by(Name) %>% mutate(Counter = row_number() - 1)` or `your_table %>% group_by(Name) %>% mutate(Counter = row_number() - 1) -> your_table` – Jon Spring Oct 20 '21 at 19:19
  • Thank you so much !!! I realized it after replying! – KLB Oct 20 '21 at 21:16

0 Answers0