In the example below I add a variable [id4] which sequentially numbers the purchases of the respondent e.g. respondent 1001 has 2 purchases, 2345 has 1 purchase, etc.
I also want to number each respondent with a sequential number as shown at bottom - how is done in tidyverse thinking?
Thanking you in advance.
dfMut <- read.table(text = "personid date measurement
1001 x 23
1001 x 32
2345 y 21
3856 x 23
3856 z 23
3856 y 23", header=TRUE)
dfMut %>% group_by(personid) %>% mutate(id4 = seq_along(personid))
personid date measurement id4
1 1001 x 23 1
2 1001 x 32 2
3 2345 y 21 1
4 3856 x 23 1
5 3856 z 23 2
6 3856 y 23 3
I want to add id5 as shown below ---
personid date measurement id4 id5
1 1001 x 23 1 1
2 1001 x 32 2 1
3 2345 y 21 1 2
4 3856 x 23 1 3
5 3856 z 23 2 3
6 3856 y 23 3 3