I am not entirely sure what you want to achieve. Below is my guess, but please feel free to provide additional information as to what you are expecting the end-result to look like.
(Note that I split the subject variable into a subject and a study variable.)
library(tidyverse)
clinical <- tibble(
subject = c("subject1_study1" , "subject1_study1", "subject1_study1", "subject2_study1", "subject2_study1", "subject2_study1", "subject3_study1", "subject3_study1"),
event = c("V1", "V2", "V3", "V1", "V2", "V3", "V1", "V2"),
treatment_group = c(1, NA, NA, NA, 2, NA, NA, 1)
)
clinical %>%
separate(subject, into = c("subject", "study"), extra = "drop", remove = FALSE) %>%
fill(treatment_group)
#> # A tibble: 8 × 4
#> event subject study treatment_group
#> <chr> <chr> <chr> <dbl>
#> 1 V1 subject1 study1 1
#> 2 V2 subject1 study1 1
#> 3 V3 subject1 study1 1
#> 4 V1 subject2 study1 1
#> 5 V2 subject2 study1 2
#> 6 V3 subject2 study1 2
#> 7 V1 subject3 study1 2
#> 8 V2 subject3 study1 1
Created on 2021-10-11 by the reprex package (v2.0.1)
This simply fills the values missing for the treatment_group variable with the preceding value. Might not be what you are after; if not, please provide some further context so that we can help you out.