I want to add ID variable in this data. If receipt_ids are sequenced numbers, then those have same IDs.
CUST_NO_ID receipt_id dollar
12 29 20.84
12 30 20.21
12 86 24.50
12 87 20.68
12 108 25.79
12 109 24.93
12 125 20.63
12 126 9.90
19 193 69.48
19 194 46.88
here is my desired result
CUST_NO_ID receipt_id dollar ID
12 29 20.84 1
12 30 20.21 1
12 86 24.50 2
12 87 20.68 2
12 108 25.79 3
12 109 24.93 3
12 110 24.93 3
12 125 20.63 4
12 126 9.90 4
19 193 69.48 5
19 194 46.88 6
Assuming your data frame has already been sorted by
CUST_NO_ID
andreceipt_id
, you can usecumsum
on a conditional vector where TRUE indicates the position a new ID should be created:This does it
We can use
data.table
Or use the
shift
Had a similar notion to @Psidom, but he beat me to the punch with
cumsum
. Here's adplyr
solution. Adding ingroup_by
can give you added flexibility if you want to restart ids by customer number.