Skip to contents

Fill experimental, treatment or symptom information within an infection episode

Usage

cg_annotate_episodes_fill(
  cg,
  col_to_fill,
  col_to_return,
  .direction = "down",
  episode_numbers_col = episode_number
)

Arguments

cg

a chronogram

col_to_fill

the name of the column to fill within each episode

col_to_return

the name of the returned, filled column

.direction

See tidyr::fill(), provide quoted.

episode_numbers_col

The column name to use for episode numbers. Default is episode_number, unquoted.

Value

a chronogram, with episode data filled within each episode.

Examples

library(dplyr)

data("built_smallstudy")
cg <- built_smallstudy$chronogram

## add infections to chronogram ##
cg <- cg_add_experiment(
  cg,
  built_smallstudy$infections_to_add
)

## annotate infections ## 
cg <- cg_annotate_episodes_find(
   cg,
   infection_cols = c("LFT", "PCR", "symptoms"),
   infection_present = c("pos", "Post", "^severe")
)
#> Parsed: infection_cols and infection_present
#>           
#> Searching in the [[column]], for the "text": 
#> 
#> stringr::str_detect(.data[["LFT"]], "pos") ~ "yes"
#> 
#> stringr::str_detect(.data[["PCR"]], "Post") ~ "yes"
#> 
#> stringr::str_detect(.data[["symptoms"]], "^severe") ~ "yes"
#> 
#> 
#> ...detecting will be exact.
#>           Capitals, spelling etc must be precise
#> 
#> Joining with `by = join_by(calendar_date, elig_study_id)`

## assign variants ##
cg <- cg %>%
mutate(
episode_variant =
case_when(
    # "is an episode" & "PCR positive" -> Delta #
    (!is.na(episode_number)) & PCR == "Pos" ~ "Delta",
    # "is an episode" & "PCR unavailable" -> Anc/Delta #
    (!is.na(episode_number)) & PCR == "not tested" ~ "Anc/Alpha"
)
)
## ^ this gives a variant call on a SINGLE row of each episode

## fill the variant call ##
cg <- cg %>% cg_annotate_episodes_fill(
 col_to_fill = episode_variant,
 col_to_return = episode_variant_filled,
 .direction = "updown",
 episode_numbers_col = episode_number
 )
#> Joining with `by = join_by(elig_study_id, episode_number)`