String Formatting
String formatting in R is typically done using the paste() or paste0() functions to combine text and variables, and sprintf() for formatted numeric output.
name <- "Dr. Santos"
diagnosis <- "Hypertension"
message <- paste("Patient seen by", name, "with", diagnosis)
print(message)
# Output
# "Patient seen by Dr. Santos with Hypertension"
To format numbers (e.g., decimals), use sprintf():
glucose <- 92.457
formatted_msg <- sprintf("Fasting blood glucose: %.1f mg/dL", glucose)
print(formatted_msg) # Fasting blood glucose: 92.5 mg/dL
R includes functions to clean or modify text — perfect for cleaning patient notes or survey entries.
text <- " Hypertension Stage II "
print(toupper(text)) # " HYPERTENSION STAGE II "
print(tolower(text)) # " hypertension stage ii "
print(trimws(text)) # "Hypertension Stage II" (removes whitespace)
print(gsub("Stage II", "Stage I", text)) # " Hypertension Stage I " (Replaces text)
medications <- "Aspirin,Metformin,Lisinopril"
med_list <- strsplit(medications, ",")[[1]] # Split into a vector
rejoined <- paste(med_list, collapse = " | ") # Join back with a separator
print(med_list) # "Aspirin" "Metformin" "Lisinopril"
print(rejoined) # "Aspirin | Metformin | Lisinopril"
String formatting helps you present data in clear, readable, and professional ways — whether you’re summarizing patient data, generating reports, or printing analysis results. Clean text formatting is essential in medical reporting and data communication.