Say I have this string:
fruits <- c("apple", "pear", "orange", "banana", "melon")
I can collapse each element into a string like this:
cat(paste(fruits, collapse = ", "))
But how can I insert and before the last element? Note that my real strings vary in length.
Output should be:
apple, pear, orange, banana and melon
You may use sub
> cat(sub("(.*),", "\\1 and",paste(fruits, collapse = ", ")))
apple, pear, orange, banana and melon
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With