Here is what finally worked with any string (I called it "final"):
final <- "B,C,A,C,C,A,B"
func_ignore_last_A <- function(seq) {
if (length(seq) > 0 && !is.na(seq)) {
vec <- str_split(seq, ",")[[1]]
if ("A" %in% vec) {
last_a_index <- tail(which(vec == "A"), 1)
vec <- vec[-last_a_index]
}
return(paste(vec, collapse = ","))
} else {
return(seq)
}
}
func_ignore_last_A(final)