how to select/calculate the data ?

antibioticDT.pdf (75.8 KB)

How to use shift or other function to calculate the last day the antibiotic was given to a patient.
Call the new variable last_administration_day .

Please specify your data in script-form as done below.
Assuming that you are interested in all types of antibiotic you could do:

suppressPackageStartupMessages( 
  library(dplyr)
)

data1 <- data.frame(
  patient_id = c(1,1,8),
  day_given = c(2,4,1),
  antibiotic_type = c('cipro','cipro','doxy'),
  route = c("IV","IV","PO")
)

print(data1)
#>   patient_id day_given antibiotic_type route
#> 1          1         2           cipro    IV
#> 2          1         4           cipro    IV
#> 3          8         1            doxy    PO

data1 |>
  group_by(patient_id) |>
  arrange(patient_id,day_given) |>
  filter(row_number()==n() )
#> # A tibble: 2 × 4
#> # Groups:   patient_id [2]
#>   patient_id day_given antibiotic_type route
#>        <dbl>     <dbl> <chr>           <chr>
#> 1          1         4 cipro           IV   
#> 2          8         1 doxy            PO
Created on 2023-05-06 with reprex v2.0.2

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.