Hi,
I have this simple df
source <- data.frame(
stringsAsFactors = FALSE,
RegNo = c(637,637,637,637,537,537,
537,539,539,539,539,783,783,783,783,767,767,767,
767,767,592,772,772,772,772),
RegDate = c("2016-03-16 00:00:00",
"2016-03-16 00:00:00","2016-03-16 00:00:00",
"2016-03-16 00:00:00","2015-03-03 00:00:00","2015-03-03 00:00:00",
"2015-03-03 00:00:00","2016-05-04 00:00:00",
"2016-05-04 00:00:00","2016-05-04 00:00:00","2016-05-04 00:00:00",
"2018-11-30 00:00:00","2018-11-30 00:00:00",
"2018-11-30 00:00:00","2018-11-30 00:00:00","2016-01-29 00:00:00",
"2016-01-29 00:00:00","2016-01-29 00:00:00",
"2016-01-29 00:00:00","2016-01-29 00:00:00","2014-03-12 00:00:00",
"2014-03-12 00:00:00","2014-03-12 00:00:00",
"2014-03-12 00:00:00","2014-03-12 00:00:00"),
Mileage = c(105682,105682,103193,101668,
91960,91960,90670,64331,61258,61258,75047,6262,
5846,7,5,80571,78211,77532,77532,75382,83908,
88400,80050,79239,76287)
)
I know how to add a variable showing count of records for each RegNo
library(dplyr)
xxx <- source %>%
add_count(RegNo, name = "Visits") %>%
mutate(Visits = Visits)
xxx
but how can I do it for RegNo and Mileage?
In my result Visits should be following:
- 2
- 2
- 1
- 1
- 2
- 2
- 1
...