gnomeR icon indicating copy to clipboard operation
gnomeR copied to clipboard

Add function to extract patient ID from sample ID

Open karissawhiting opened this issue 1 year ago • 0 comments

library(dplyr)
library(stringr)

extract_patient_id <- function(data) {
  if (all(str_detect(data$sample_id, "^P-\\d{1,}-T.*"))) {
    return(data %>%
      mutate(patient_id = str_replace(sample_id, "-T.*", "")))
  } else {
    # If any value does not match the format, raise an error
    cli::cli_abort("Some {.code sample_id} values do not match the expected IMPACT sample format.")
  }
} 

# Example
df <- data.frame(
  sample_id =
    c("P-0000071-T01-IM3", "P-0000072-T02-IM4", "P-0000073-T03-IM5")
)
transformed_df <- extract_patient_id(df)
print(transformed_df)

karissawhiting avatar Nov 01 '23 15:11 karissawhiting