1

I want to compute a column that counts the orders by Date of customers.

Here some toy code:

Date <- as.Date(c('2006-08-30','2006-08-23', '2006-09-06', '2006-09-13', '2006-09-20')) 
ID <- c("x1","x1","X2","X3","x1") 
TransNo<-c("123","124","125","126","127")

df<-data.frame(ID,Date,TransNo) 

My expected result looks like this, where "Times" is the desired var:

ID       Date TransNo Times
1 x1 2006-08-30     123     2
2 x1 2006-08-23     124     1
3 X2 2006-09-06     125     1
4 X3 2006-09-13     126     1
5 x1 2006-09-20     127     3

I tried some aggregate and dplyr solutions with count / n or length. Not sure what solves this problem but it cant be rocket-science.

Any help would be great! Thank you all in advance

1 Answers1

0

Does this do what you want?

df<-data.frame(ID,Date,TransNo) %>%
  arrange(Date) %>%
  group_by(ID) %>%
  mutate(dummy = 1) %>%
  mutate(times = cumsum(dummy)) %>%
  select(-dummy)

returns

 ID    Date       TransNo times
  <fct> <date>     <fct>   <dbl>
1 x1    2006-08-23 124         1
2 x1    2006-08-30 123         2
3 X2    2006-09-06 125         1
4 X3    2006-09-13 126         1
5 x1    2006-09-20 127         3
nogbad
  • 435
  • 4
  • 15