6  mutate exercises

Author

Thomas Neitmann

6.1 Setup

library(dplyr)
library(lubridate)
dm <- readRDS("data/dm.rds")
ae <- readRDS("data/ae.rds")

6.2 Exercise 1

A treatment emergent adverse event is defined as an adverse event whose start date is on or after the treatment start date (TRTSDT) and at the latest starts 7 days after the treatment end date (TRTEDT). Given this definition calculate TRTEMFL.

Hint: Turn the --DTC variables into proper dates first using the ymd() function.

Show solution
ae %>% 
  mutate(
    ASTDT = ymd(AESTDTC),
    AENDT = ymd(AEENDTC),
    TRTEMFL = if_else(ASTDT >= TRTSDT & ASTDT <= TRTEDT + 7, "Y", NA_character_)
  ) %>% 
  select(USUBJID, ASTDT, AENDT, TRTSDT, TRTEDT, TRTEMFL)
Warning: 19 failed to parse.
# A tibble: 1,191 × 6
   USUBJID     ASTDT      AENDT      TRTSDT     TRTEDT     TRTEMFL
   <chr>       <date>     <date>     <date>     <date>     <chr>  
 1 01-701-1015 2014-01-03 NA         2014-01-02 2014-07-02 Y      
 2 01-701-1015 2014-01-03 NA         2014-01-02 2014-07-02 Y      
 3 01-701-1015 2014-01-09 2014-01-11 2014-01-02 2014-07-02 Y      
 4 01-701-1023 2012-08-26 NA         2012-08-05 2012-09-01 Y      
 5 01-701-1023 2012-08-07 2012-08-30 2012-08-05 2012-09-01 Y      
 6 01-701-1023 2012-08-07 NA         2012-08-05 2012-09-01 Y      
 7 01-701-1023 2012-08-07 2012-08-30 2012-08-05 2012-09-01 Y      
 8 01-701-1028 2013-07-21 NA         2013-07-19 2014-01-14 Y      
 9 01-701-1028 2013-08-08 NA         2013-07-19 2014-01-14 Y      
10 01-701-1034 2014-08-27 NA         2014-07-01 2014-12-30 Y      
# … with 1,181 more rows

6.3 Exercise 2

Create a new variable REGION1 based upon COUTRY as shown in the table below.

Countries Region
Mexico, USA, Canada North America
Spain, Greece, Germany, Switzerland Europe
China, Japan Asia
Show solution
dm %>% 
  mutate(
    REGION1 = case_when(
      COUNTRY %in% c("Mexico", "USA", "Canada") ~ "North America",
      COUNTRY %in% c("Spain", "Greece", "Germany", "Switzerland") ~ "Europe",
      COUNTRY %in% c("China", "Japan") ~ "Asia"
    )
  ) %>% 
  select(USUBJID, COUNTRY, REGION1)
# A tibble: 306 × 3
   USUBJID     COUNTRY REGION1      
   <chr>       <chr>   <chr>        
 1 01-701-1015 USA     North America
 2 01-701-1023 USA     North America
 3 01-701-1028 USA     North America
 4 01-701-1033 USA     North America
 5 01-701-1034 USA     North America
 6 01-701-1047 USA     North America
 7 01-701-1057 USA     North America
 8 01-701-1097 USA     North America
 9 01-701-1111 USA     North America
10 01-701-1115 USA     North America
# … with 296 more rows