Some modelling software needs group data as an index. This means instead of c("AU", "AU", "UK", "UK", "NZ") you need c(1, 1, 2, 2, 3), corresponding to each of the groups. This function makes it simpler to add that information to a dataframe.

add_group_id(.data, group)

Arguments

.data

data.frame

group

group to add the ID for

Value

data.frame with additional column named ".group_id", where "group" is named after the variable you would like to add the group ID for.

Examples

malaria_africa_ts %>% add_group_id(country)
#> # A tsibble: 1,046 x 16 [1D]
#> # Key:       country [42]
#>    who_region who_subregion country .country_id date       month_num positive
#>    <fct>      <fct>         <fct>         <int> <date>         <dbl>    <dbl>
#>  1 AFRO       AFRO-W        Angola            1 1989-06-01       120     15.8
#>  2 AFRO       AFRO-W        Angola            1 2005-11-01       372     82  
#>  3 AFRO       AFRO-W        Angola            1 2006-04-01       300    102  
#>  4 AFRO       AFRO-W        Angola            1 2006-11-01       384     41  
#>  5 AFRO       AFRO-W        Angola            1 2006-12-01       396    173  
#>  6 AFRO       AFRO-W        Angola            1 2007-01-01       276    216  
#>  7 AFRO       AFRO-W        Angola            1 2007-02-01       288     42  
#>  8 AFRO       AFRO-W        Angola            1 2007-03-01       300    119  
#>  9 AFRO       AFRO-W        Angola            1 2011-01-01       324      1  
#> 10 AFRO       AFRO-W        Angola            1 2011-02-01       336    148  
#> # … with 1,036 more rows, and 9 more variables: examined <int>, pr <dbl>,
#> #   avg_lower_age <dbl>, continent_id <fct>, country_id <fct>, year <int>,
#> #   month <int>, avg_upper_age <dbl>, species <fct>