I am doing some data cleaning/ formatting, and I would like to add a unique identifier to each record by name and then by date. For example, "Bob" may have four check-in dates, two of which are the same. For a case like this, I want to give him three different (sequential) ID numbers.
Here is the closest I have gotten to my desired result:
An example data set I created:
tst <- data_frame(
name = c("Bob", "Sam", "Roger", "Stacy", "Roger", "Roger", "Sam", "Bob", "Sam", "Stacy", "Bob", "Stacy", "Roger", "Bob"),
date = as.Date(c("2009-07-03", "2010-08-12", "2009-07-03", "2016-04-01", "2002-01-03", "2019-02-10", "2005-04-17", "2009-07-03", "2010-09-21", "2012-11-12", "2015-12-31", "2014-10-10", "2015-06-02", "2003-08-21")),
amount = round(runif(14, 0, 100), 2)
)
Generating a check_in_number
variable...
tst2 <- tst %>%
arrange(date) %>%
group_by(name, date) %>%
mutate(check_in_number = row_number())
The line above will generate check_in_number
for Bob as 1
, 1
, 2
, 1
in that order. I would instead like the output to be 1
, 2
, 2
, 3
. In other words. I would like check-in instances on the same date to be considered a single check-in.
Is this possible with tidyverse? Am I overlooking a simple method for this?
There is a similar question here, but I am leaving this up because the problem I had involved an ordered date variable on which I was arranging the data. In other words, my data required my new variable to be consecutive.
How to number/label data-table by group-number from group_by?