Numbering shoppers in multi-line till slip data #2

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

Found this an it works -
dfMut <- transform(dfMut,Basketid=as.numeric(factor(personid)))

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

Sorry I'm late to the party, but if you want to stay on the tidyverse (instead of using base::transform()) you could do something like this

library(dplyr)

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),
           id5 = group_indices())
#> # A tibble: 6 x 5
#> # Groups:   personid [3]
#>   personid date  measurement   id4   id5
#>      <int> <fct>       <int> <int> <int>
#> 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

Created on 2019-12-19 by the reprex package (v0.3.0.9000)

1 Like

Thank you. I would like to stay within tidyverse as it's more intuitive for me. I will use this solution. Regards

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.