Skip to contents

The [] and [[]] operators for linelist objects behaves like for regular data.frame or tibble, but check that tagged variables are not lost, and takes the appropriate action if this is the case (warning, error, or ignore, depending on the general option set via lost_tags_action()) .

Usage

# S3 method for linelist
[(x, i, j, drop = FALSE)

# S3 method for linelist
[(x, i, j) <- value

# S3 method for linelist
[[(x, i, j) <- value

# S3 method for linelist
$(x, name) <- value

Arguments

x

a linelist object

i

a vector of integer or logical to subset the rows of the linelist

j

a vector of character, integer, or logical to subset the columns of the linelist

drop

a logical indicating if, when a single column is selected, the data.frame class should be dropped to return a simple vector, in which case the linelist class is lost as well; defaults to FALSE

value

the replacement to be used for the entries identified in x

name

A literal character string or a name (possibly backtick quoted). For extraction, this is normally (see under ‘Environments’) partially matched to the names of the object.

Value

If no drop is happening, a linelist. Otherwise an atomic vector.

See also

Examples

if (require(outbreaks) && require(dplyr) && require(magrittr)) {
  ## create a linelist
  x <- measles_hagelloch_1861 %>%
    tibble() %>%
    make_linelist(
      id = "case_ID",
      date_onset = "date_of_prodrome",
      age = "age",
      gender = "gender"
    ) %>%
    mutate(result = if_else(is.na(date_of_death), "survived", "died")) %>%
    set_tags(outcome = "result") %>%
    rename(identifier = case_ID)
  x

  ## dangerous removal of a tagged column setting it to NULL issues a warning
  x[, 1] <- NULL
  x

  x[[2]] <- NULL
  x

  x$age <- NULL
  x
}
#> Warning: The following tags have lost their variable:
#>  id:identifier
#> Warning: The following tags have lost their variable:
#>  date_onset:date_of_prodrome
#> Warning: The following tags have lost their variable:
#>  age:age
#> 
#> // linelist object
#> # A tibble: 188 × 10
#>    infector date_of_rash date_of_death gender family_ID class complications
#>       <int> <date>       <date>        <fct>      <int> <fct> <fct>        
#>  1       45 1861-11-25   NA            f             41 1     yes          
#>  2       45 1861-11-27   NA            f             41 1     yes          
#>  3      172 1861-12-02   NA            f             41 0     yes          
#>  4      180 1861-11-28   NA            m             61 2     yes          
#>  5       45 1861-11-27   NA            f             42 1     yes          
#>  6      180 1861-11-29   NA            m             42 2     yes          
#>  7       42 1861-11-28   NA            m             26 0     yes          
#>  8       45 1861-11-26   NA            m             44 1     yes          
#>  9      182 1861-11-30   NA            m             44 2     yes          
#> 10       45 1861-11-25   NA            f             29 1     yes          
#> # ℹ 178 more rows
#> # ℹ 3 more variables: x_loc <dbl>, y_loc <dbl>, result <chr>
#> 
#> // tags: gender:gender, outcome:result