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)))
cderv
December 18, 2019, 10:52pm
3
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:
If your question has been answered, don't forget to mark the solution!
How do I mark a solution?
Find the reply you want to mark as the solution and look for the row of small gray icons at the bottom of that reply. Click the one that looks like a box with a checkmark in it:
[image]
Hovering over the mark solution button shows the label, "Select if this reply solves the problem". If you don't see the mark solution button, try clicking the three dots button ( ••• ) to expand the full set of options.
When a solution is chosen, the icon turns green and the hover label changes to: "Unselect if this reply no longer solves the problem". Success!
[solution_reply_author]
…
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
system
Closed
December 26, 2019, 6:26am
6
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.