column with count of matching dates

Hi there,

I'm sure this is rather simple. I seem to have confused myself with a solution. :worried:

I'm seeking to add a count column of matching date values in a new df column. While I've successful produced the counts using tally, group_by etc, I really need the data in its original table form with original rows intact.


library(tidyverse,quietly=TRUE)
#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from 
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang

## List of various days
days <- as_tibble(as.Date(c("2019-01-01","2019-01-02", "2019-01-02","2019-01-03",
                                 "2019-01-03","2019-01-03", "2019-01-04","2019-01-04",
                                 "2019-01-04","2019-01-04", "2019-01-05","2019-01-05",
                                 "2019-01-05","2019-01-05", "2019-01-05")))
#> Warning: Calling `as_tibble()` on a vector is discouraged, because the behavior is likely to change in the future. Use `tibble::enframe(name = NULL)` instead.
#> This warning is displayed once per session.

# Column required with number of matching days from day column
(days <- add_column(days, n_days = c(1,2,2,3,3,3,4,4,4,4,5,5,5,5,5)))
#> # A tibble: 15 x 2
#>    value      n_days
#>    <date>      <dbl>
#>  1 2019-01-01      1
#>  2 2019-01-02      2
#>  3 2019-01-02      2
#>  4 2019-01-03      3
#>  5 2019-01-03      3
#>  6 2019-01-03      3
#>  7 2019-01-04      4
#>  8 2019-01-04      4
#>  9 2019-01-04      4
#> 10 2019-01-04      4
#> 11 2019-01-05      5
#> 12 2019-01-05      5
#> 13 2019-01-05      5
#> 14 2019-01-05      5
#> 15 2019-01-05      5

Created on 2019-05-23 by the reprex package (v0.3.0)

So essentially a column with the count of matching dates.

Any help or pointers appreciated.

Regards

Is this what you want to do?

library(tidyverse)
days <- as_tibble(as.Date(c("2019-01-01","2019-01-02", "2019-01-02","2019-01-03",
                            "2019-01-03","2019-01-03", "2019-01-04","2019-01-04",
                            "2019-01-04","2019-01-04", "2019-01-05","2019-01-05",
                            "2019-01-05","2019-01-05", "2019-01-05")))
#> Warning: Calling `as_tibble()` on a vector is discouraged, because the behavior is likely to change in the future. Use `tibble::enframe(name = NULL)` instead.
#> This warning is displayed once per session.
days %>% 
    group_by(value) %>% 
    add_count(name = "n_days")
#> # A tibble: 15 x 2
#> # Groups:   value [5]
#>    value      n_days
#>    <date>      <int>
#>  1 2019-01-01      1
#>  2 2019-01-02      2
#>  3 2019-01-02      2
#>  4 2019-01-03      3
#>  5 2019-01-03      3
#>  6 2019-01-03      3
#>  7 2019-01-04      4
#>  8 2019-01-04      4
#>  9 2019-01-04      4
#> 10 2019-01-04      4
#> 11 2019-01-05      5
#> 12 2019-01-05      5
#> 13 2019-01-05      5
#> 14 2019-01-05      5
#> 15 2019-01-05      5
2 Likes

That's excellent. Perfect once again. Cheers.

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