I think there may be two issues here: If the date is left as character, as in @siddharthprabhu' s code, the ordering will be alphabetical, rather than by date. This is fixed in @vinaychuri's and @joels's code, but it depends on which code @dstokar copied to use. The other issue is that a grouped table may be unpredictably reordered by a subsequent operation, so that slice()
may not pick the intended rows. I think @joels's code should work, but here's another version to try, @dstokar:
library(tidyverse)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
df <- tibble::tribble(
~subjid, ~Date, ~SBP, ~Visit_Type,
1, "15-Jan-19", 125, "Screening",
1, "16-Jan-19", 130, "Screening",
1, "17-Jan-19", 127, NA,
1, "18-Jan-19", 120, NA,
2, "9-Jan-19", 145, "Screening",
2, "10-Jan-19", 130, "Screening",
2, "11-Jan-19", 140, NA,
2, "12-Jan-19", 120, NA,
3, "10-Feb-19", 145, "Screening",
3, "12-Feb-19", NA, "Screening",
3, "13-Feb-19", 140, NA,
3, "15-Feb-19", 120, NA
)
# summary version
df %>%
mutate(Date = dmy(Date)) %>%
filter(Visit_Type == 'Screening', !is.na(SBP)) %>%
group_by(subjid, Visit_Type) %>%
arrange(desc(Date)) %>%
slice(1) %>%
ungroup() %>%
mutate(Visit_Type = 'Baseline')
#> # A tibble: 3 x 4
#> subjid Date SBP Visit_Type
#> <dbl> <date> <dbl> <chr>
#> 1 1 2019-01-16 130 Baseline
#> 2 2 2019-01-10 130 Baseline
#> 3 3 2019-02-10 145 Baseline
# full table version
df %>%
mutate(Date = dmy(Date)) %>%
group_by(subjid, Visit_Type, is.na(SBP)) %>%
arrange(desc(Date)) %>%
mutate(group_order = row_number()) %>%
ungroup() %>%
mutate(
Visit_Type =
if_else(
Visit_Type == 'Screening' & !is.na(SBP) & group_order == 1,
'Baseline',
Visit_Type
)
) %>%
select(-`is.na(SBP)`, -group_order)
#> # A tibble: 12 x 4
#> subjid Date SBP Visit_Type
#> <dbl> <date> <dbl> <chr>
#> 1 3 2019-02-15 120 <NA>
#> 2 3 2019-02-13 140 <NA>
#> 3 3 2019-02-12 NA Screening
#> 4 3 2019-02-10 145 Baseline
#> 5 1 2019-01-18 120 <NA>
#> 6 1 2019-01-17 127 <NA>
#> 7 1 2019-01-16 130 Baseline
#> 8 1 2019-01-15 125 Screening
#> 9 2 2019-01-12 120 <NA>
#> 10 2 2019-01-11 140 <NA>
#> 11 2 2019-01-10 130 Baseline
#> 12 2 2019-01-09 145 Screening
Created on 2020-03-06 by the reprex package (v0.3.0)
Does this work in the right way?