Skip to contents

These functions determine which items in a vector can be considered (the start of) a new episode, based on the argument episode_days. This can be used to determine clinical episodes for any epidemiological analysis. The get_episode() function returns the index number of the episode per group, while the is_new_episode() function returns values TRUE/FALSE to indicate whether an item in a vector is the start of a new episode.

Usage

get_episode(x, episode_days, ...)

is_new_episode(x, episode_days, ...)

Arguments

x

vector of dates (class Date or POSIXt), will be sorted internally to determine episodes

episode_days

required episode length in days, can also be less than a day or Inf, see Details

...

ignored, only in place to allow future extensions

Value

  • get_episode(): a double vector

  • is_new_episode(): a logical vector

Details

Dates are first sorted from old to new. The oldest date will mark the start of the first episode. After this date, the next date will be marked that is at least episode_days days later than the start of the first episode. From that second marked date on, the next date will be marked that is at least episode_days days later than the start of the second episode which will be the start of the third episode, and so on. Before the vector is being returned, the original order will be restored.

The first_isolate() function is a wrapper around the is_new_episode() function, but is more efficient for data sets containing microorganism codes or names and allows for different isolate selection methods.

The dplyr package is not required for these functions to work, but these functions do support variable grouping and work conveniently inside dplyr verbs such as filter(), mutate() and summarise().

See also

Examples

# `example_isolates` is a data set available in the AMR package.
# See ?example_isolates
df <- example_isolates[sample(seq_len(2000), size = 200), ]

get_episode(df$date, episode_days = 60) # indices
#>   [1] 14 30 51 63 66  3 25 25 10 50 17 32 20 64 19 41 45 62 25 18 24 26 28  6 63
#>  [26] 47 32 10 16 28 23  2  4 66 62  8 41 13 15 56  9 22 34 51 15 65 45  1 53 57
#>  [51] 29 39 12 52 12 33 58 57 48 51 40 27 30 18 36 43 13  7 65 60 13 25 16 21 62
#>  [76] 66 39 22 39  7  8 24 60 19 27 27 58  5 49  6 62 23 15 39 10 66 63  7 50  5
#> [101] 60 29 67 51 52 57 34 48 60 18 65 55 48 51 66 12 20 49 41 65 41 67 53 39 11
#> [126] 55 65 66 68 13 27 43 34 46 51  3 13 11 23 31 65 34 11 37 10 54 16 66 24 50
#> [151] 38 10  5  8 59 30 49 63 59  9 36 44 38 28 30 65  5  6 66 10 30  5 26  1 42
#> [176] 18 63 66 14 60 64 35 55 47 13 43 21 66 61  8 61 54 41 64 12 65 16 40 10 52
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
#>   [1]  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE
#>  [13] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE
#>  [25] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE
#>  [37] FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE
#>  [49]  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE
#>  [61] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE
#>  [73] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE
#>  [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#>  [97] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE
#> [109] FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
#> [121] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE
#> [133] FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE
#> [145]  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
#> [157]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE
#> [169] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE
#> [181] FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
#> [193] FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE

# filter on results from the third 60-day episode only, using base R
df[which(get_episode(df$date, 60) == 3), ]
#> # A tibble: 2 × 46
#>   date       patient   age gender ward     mo           PEN   OXA   FLC   AMX  
#>   <date>     <chr>   <dbl> <chr>  <chr>    <mo>         <rsi> <rsi> <rsi> <rsi>
#> 1 2002-10-20 F35553     51 M      ICU      B_STPHY_AURS S     NA    S     NA   
#> 2 2002-11-18 956065     89 F      Clinical B_ESCHR_COLI R     NA    NA    NA   
#> # … with 36 more variables: AMC <rsi>, AMP <rsi>, TZP <rsi>, CZO <rsi>,
#> #   FEP <rsi>, CXM <rsi>, FOX <rsi>, CTX <rsi>, CAZ <rsi>, CRO <rsi>,
#> #   GEN <rsi>, TOB <rsi>, AMK <rsi>, KAN <rsi>, TMP <rsi>, SXT <rsi>,
#> #   NIT <rsi>, FOS <rsi>, LNZ <rsi>, CIP <rsi>, MFX <rsi>, VAN <rsi>,
#> #   TEC <rsi>, TCY <rsi>, TGC <rsi>, DOX <rsi>, ERY <rsi>, CLI <rsi>,
#> #   AZM <rsi>, IPM <rsi>, MEM <rsi>, MTR <rsi>, CHL <rsi>, COL <rsi>,
#> #   MUP <rsi>, RIF <rsi>

# the functions also work for less than a day, e.g. to include one per hour:
get_episode(c(
  Sys.time(),
  Sys.time() + 60 * 60
),
episode_days = 1 / 24
)
#> [1] 1 2

# \donttest{
if (require("dplyr")) {
  # is_new_episode() can also be used in dplyr verbs to determine patient
  # episodes based on any (combination of) grouping variables:
  df %>%
    mutate(condition = sample(
      x = c("A", "B", "C"),
      size = 200,
      replace = TRUE
    )) %>%
    group_by(condition) %>%
    mutate(new_episode = is_new_episode(date, 365)) %>%
    select(patient, date, condition, new_episode)
}
#> # A tibble: 200 × 4
#> # Groups:   condition [3]
#>    patient date       condition new_episode
#>    <chr>   <date>     <chr>     <lgl>      
#>  1 219024  2005-02-17 C         FALSE      
#>  2 A31059  2008-12-04 C         FALSE      
#>  3 D53062  2014-07-28 A         FALSE      
#>  4 305134  2016-10-28 C         FALSE      
#>  5 D80438  2017-07-03 C         FALSE      
#>  6 F35553  2002-10-20 C         FALSE      
#>  7 ED4982  2007-07-13 B         FALSE      
#>  8 122506  2007-08-11 A         FALSE      
#>  9 05B00F  2004-05-11 A         TRUE       
#> 10 8F77B2  2014-01-14 C         TRUE       
#> # … with 190 more rows
if (require("dplyr")) {
  df %>%
    group_by(ward, patient) %>%
    transmute(date,
      patient,
      new_index = get_episode(date, 60),
      new_logical = is_new_episode(date, 60)
    )
}
#> # A tibble: 200 × 5
#> # Groups:   ward, patient [180]
#>    ward     date       patient new_index new_logical
#>    <chr>    <date>     <chr>       <dbl> <lgl>      
#>  1 Clinical 2005-02-17 219024          1 TRUE       
#>  2 Clinical 2008-12-04 A31059          1 TRUE       
#>  3 Clinical 2014-07-28 D53062          1 TRUE       
#>  4 ICU      2016-10-28 305134          1 TRUE       
#>  5 Clinical 2017-07-03 D80438          1 TRUE       
#>  6 ICU      2002-10-20 F35553          2 TRUE       
#>  7 ICU      2007-07-13 ED4982          1 TRUE       
#>  8 Clinical 2007-08-11 122506          1 TRUE       
#>  9 ICU      2004-05-11 05B00F          1 TRUE       
#> 10 Clinical 2014-01-14 8F77B2          1 TRUE       
#> # … with 190 more rows
if (require("dplyr")) {
  df %>%
    group_by(ward) %>%
    summarise(
      n_patients = n_distinct(patient),
      n_episodes_365 = sum(is_new_episode(date, episode_days = 365)),
      n_episodes_60 = sum(is_new_episode(date, episode_days = 60)),
      n_episodes_30 = sum(is_new_episode(date, episode_days = 30))
    )
}
#> # A tibble: 3 × 5
#>   ward       n_patients n_episodes_365 n_episodes_60 n_episodes_30
#>   <chr>           <int>          <int>         <int>         <int>
#> 1 Clinical          112             14            55            73
#> 2 ICU                56             12            36            46
#> 3 Outpatient         12              8            11            12
if (require("dplyr")) {

  # grouping on patients and microorganisms leads to the same
  # results as first_isolate() when using 'episode-based':
  x <- df %>%
    filter_first_isolate(
      include_unknown = TRUE,
      method = "episode-based"
    )

  y <- df %>%
    group_by(patient, mo) %>%
    filter(is_new_episode(date, 365)) %>%
    ungroup()

  identical(x, y)
}
#> Including isolates from ICU.
#> [1] FALSE
if (require("dplyr")) {

  # but is_new_episode() has a lot more flexibility than first_isolate(),
  # since you can now group on anything that seems relevant:
  df %>%
    group_by(patient, mo, ward) %>%
    mutate(flag_episode = is_new_episode(date, 365)) %>%
    select(group_vars(.), flag_episode)
}
#> # A tibble: 200 × 4
#> # Groups:   patient, mo, ward [188]
#>    patient mo           ward     flag_episode
#>    <chr>   <mo>         <chr>    <lgl>       
#>  1 219024  B_ESCHR_COLI Clinical TRUE        
#>  2 A31059  B_STRPT_MTNS Clinical TRUE        
#>  3 D53062  B_HMPHL_INFL Clinical TRUE        
#>  4 305134  B_PROTS_VLGR ICU      TRUE        
#>  5 D80438  B_STPHY_AURS Clinical TRUE        
#>  6 F35553  B_STPHY_AURS ICU      TRUE        
#>  7 ED4982  B_ESCHR_COLI ICU      TRUE        
#>  8 122506  B_STPHY_AURS Clinical TRUE        
#>  9 05B00F  F_CANDD_ALBC ICU      TRUE        
#> 10 8F77B2  B_ENTRC_FACM Clinical TRUE        
#> # … with 190 more rows
# }