add count for more than one variable

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:

  1. 2
  2. 2
  3. 1
  4. 1
  5. 2
  6. 2
  7. 1
    ...

I think you just need to list the two variables in add_count().

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))
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
  source |> add_count(RegNo, Mileage, name = "Visits")
#>    RegNo             RegDate Mileage Visits
#> 1    637 2016-03-16 00:00:00  105682      2
#> 2    637 2016-03-16 00:00:00  105682      2
#> 3    637 2016-03-16 00:00:00  103193      1
#> 4    637 2016-03-16 00:00:00  101668      1
#> 5    537 2015-03-03 00:00:00   91960      2
#> 6    537 2015-03-03 00:00:00   91960      2
#> 7    537 2015-03-03 00:00:00   90670      1
#> 8    539 2016-05-04 00:00:00   64331      1
#> 9    539 2016-05-04 00:00:00   61258      2
#> 10   539 2016-05-04 00:00:00   61258      2
#> 11   539 2016-05-04 00:00:00   75047      1
#> 12   783 2018-11-30 00:00:00    6262      1
#> 13   783 2018-11-30 00:00:00    5846      1
#> 14   783 2018-11-30 00:00:00       7      1
#> 15   783 2018-11-30 00:00:00       5      1
#> 16   767 2016-01-29 00:00:00   80571      1
#> 17   767 2016-01-29 00:00:00   78211      1
#> 18   767 2016-01-29 00:00:00   77532      2
#> 19   767 2016-01-29 00:00:00   77532      2
#> 20   767 2016-01-29 00:00:00   75382      1
#> 21   592 2014-03-12 00:00:00   83908      1
#> 22   772 2014-03-12 00:00:00   88400      1
#> 23   772 2014-03-12 00:00:00   80050      1
#> 24   772 2014-03-12 00:00:00   79239      1
#> 25   772 2014-03-12 00:00:00   76287      1

Created on 2023-06-09 with reprex v2.0.2

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.