I made this into a light reprex and took a shot at it. Basically, what it does is:
- Removes
pt (that looks to be a row number in your example; if it's a patient ID that would actually be the same value for "eva malso", then it could be part of the group_by, too)
- For the summarizing, it evaluates each column and gets the number of unique values
- If the total unique values (the length) is 1, then it uses the first value for the summary
- Otherwise, it pastes all of the values and collapses them with a " , " separator
This works for the example you provided. What it won't do is gracefully handle a scenario where, for instance, there are 3 records for one patient, and there are, say, two unique contact numbers. It would wind up doing the "paste" and collapsing all three...even though two of them are duplicates of each other. But, it's not clear if you would expect that to happen with much frequency. That might be addressable with a nested grouping within the summarize_at...but could get a little tricky.
library(tidyverse)
# Create a date frame to work with
df <- data.frame(
pt = c("1", "2", "3", "4"),
name = c("eva malso", "mark smith", "john doe", "eva malso"),
contact_num = c("123.456.7890", "234.567.8901", "345.678.9012", "123.456.7890"),
ins_provider = c("cigna", "bcbs", "united", "cigna"),
date_of_contact = c("1.2.21", "1.2.21", "1.3.21", "1.4.21"),
referral = c("RRRC", "DS", "CP", "RRRC"),
referral_name = c("Tyler BOV", "Susie Car1", "Jack LOL", "Tyler ASC"),
status = c("initiated", "info", "initiated", "intiated"),
staff = c("KO", "KO", "SB", "SB"),
notes = c("pt called to ask about some services. Ins info provided. Billing Solutions contacted",
"Pt interested in some night sessions. Info provided. No ins. Collected.",
"pt interested in medicine. Ins info provided. Billing solutions contacted.",
"Deductible of $14.00 Met $21.34. company process initiated")
)
# Do the de-duplication
df2 <- df %>%
select(-pt) %>%
group_by(name) %>%
summarize_all(~ if_else(length(unique(.x)) == 1,
first(.x),
paste(.x, collapse = ", ")))