Code Studio: Oxford Comma Lists (stringr)
This is the last problem in Practice: Strings, solved with the stringr package, which is now available here.
Write a function commas() that turns a vector of words into a readable list:
- one word:
apples - two words:
salt and pepper(no comma) - three or more:
red, green, and blue(commas, and "and" before the last)
The program reads one line of words separated by spaces and prints commas(words). Use str_c() with its collapse argument to join the words.
Think about the sep and collapse question from the reading: which one do you need here, and why?
This is the last problem in Practice: Strings, solved with the stringr package, which is now available here.
Write a function commas() that turns a vector of words into a readable list:
- one word:
apples - two words:
salt and pepper(no comma) - three or more:
red, green, and blue(commas, and "and" before the last)
The program reads one line of words separated by spaces and prints commas(words). Use str_c() with its collapse argument to join the words.
Think about the sep and collapse question from the reading: which one do you need here, and why?