There's not as many pre-defined summary functions for text/string data (e.g. mean(), median() etc don't make sense). But you could write your own custom summary function if you have some specific behaviour you want to use, e.g.:
library(tidyverse)
My.data <- data.frame(
stringsAsFactors=FALSE,
ReferenceNumber = c("xyz", "xyz", "abc", "abc", "abc", "abc", "abc", "abc"),
Date = c("2019-03-22", "2019-03-23", "2017-11-29", "2017-11-29", "2018-01-11", "2018-01-12", "2018-11-27","2018-11-27"),
Description = c("bla bla", "bla bla", "aaa", "aaa", "bbb", "bbb", "ccc", "ccc"),
Cost = c("High", "Low", "High", "High","High", "Low", "High", "Low"),
Cost2 = c(225, 6, 6, 51, 2, 4, 1, 14),
NewVar = c(2, 6, 6, 5, 1, 1, 4, 4)
)
custom_string_summary <- function(string_vec) {
ifelse(length(unique(string_vec)) == 1, unique(string_vec), "Unsure")
}
My.data %>%
group_by(ReferenceNumber, Date, Description) %>%
summarise(Cost = custom_string_summary(Cost))
#> # A tibble: 6 x 4
#> # Groups: ReferenceNumber, Date [6]
#> ReferenceNumber Date Description Cost
#> <chr> <chr> <chr> <chr>
#> 1 abc 2017-11-29 aaa High
#> 2 abc 2018-01-11 bbb High
#> 3 abc 2018-01-12 bbb Low
#> 4 abc 2018-11-27 ccc Unsure
#> 5 xyz 2019-03-22 bla bla High
#> 6 xyz 2019-03-23 bla bla Low
Created on 2019-10-18 by the reprex package (v0.3.0)
Here, the custom summary function says "if there's only one distinct value, use that value, otherwise say we're unsure what the value is". You could write any summary function you wanted to, really, or use paste() with the collapse argument set to "roll up" all the character values in to a single item.