Home > Enterprise >  How can i make a dense_rank start at 0 instead of 1 in R?
How can i make a dense_rank start at 0 instead of 1 in R?

Time:08-16

How can I make the rank start at zero the default starts at 1 but I need my rank to start at zero. Ithe current rank which is time and i want to be able to have a rank time 0. Such that i have time as 0, 1, 2

    library (tidyverse)
    
        test <- data.frame(distinct_id = c(001,001,001, 002, 002, 002, 033, 033, 033, 044, 
                044, 044),
        date = c("2020-01-20","2020-01-21","2020-01-22","2020-01-22","2020-01-23","2020-01- 
                 24","2020-01-22","2020-01-23","2020-01-24","2020-01-22","2020-01-23","2020- 
                 01-24"))
        
        test %>% 
          group_by(distinct_id) %>% 
          mutate(Time = dense_rank(date))
    ```

     #> # A tibble: 12 × 3
     #> # Groups:   distinct_id [4]
     #>    distinct_id date        Time
     #>          <dbl> <chr>      <int>
     #>  1           1 2020-01-20     1
     #>  2           1 2020-01-21     2
     #>  3           1 2020-01-22     3
     #>  4           2 2020-01-22     1
     #>  5           2 2020-01-23     2
     #>  6           2 2020-01-24     3
     #>  7          33 2020-01-22     1
     #>  8          33 2020-01-23     2
     #>  9          33 2020-01-24     3
     #> 10          44 2020-01-22     1
     #> 11          44 2020-01-23     2
     #> 12          44 2020-01-24     3

CodePudding user response:

You can subtract 1 from the dense_rank() output to set the baseline at 0!

library (tidyverse)

test <- data.frame(distinct_id = c(001,001,001, 002, 002, 002, 033, 033, 033, 044, 
                                   044, 044),
                   date = c("2020-01-20","2020-01-21","2020-01-22","2020-01-22","2020-01-23","2020-01-24","2020-01-22","2020-01-23","2020-01-24","2020-01-22","2020-01-23","2020-01-24"))

test %>% 
  group_by(distinct_id) %>% 
  mutate(Time = dense_rank(date)-1)
#> # A tibble: 12 × 3
#> # Groups:   distinct_id [4]
#>    distinct_id date        Time
#>          <dbl> <chr>      <dbl>
#>  1           1 2020-01-20     0
#>  2           1 2020-01-21     1
#>  3           1 2020-01-22     2
#>  4           2 2020-01-22     0
#>  5           2 2020-01-23     1
#>  6           2 2020-01-24     2
#>  7          33 2020-01-22     0
#>  8          33 2020-01-23     1
#>  9          33 2020-01-24     2
#> 10          44 2020-01-22     0
#> 11          44 2020-01-23     1
#> 12          44 2020-01-24     2

Created on 2022-08-15 by the reprex package (v2.0.1)

  • Related