串函数,将以逗号分隔的单词向量与"and"连接.在最后一句话之前 [英] stringr function to concatenate vector of words separated by comma with "and" before last word

查看:114
本文介绍了串函数,将以逗号分隔的单词向量与"and"连接.在最后一句话之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以很容易地写一个,但是没有人知道stringer(或stringi)是否已经具有一个函数,该函数将一个或多个单词的向量连接起来,这些向量之间用逗号隔开,但在最后一个单词之前加上"and"?

I know I can easily write one, but does anyone know if stringr (or stringi) already has a function that concatenates a vector of one or more words separated by commas, but with an "and" before the last word?

推荐答案

您可以使用knitr::combine_words函数

knitr::combine_words(letters[1:2])
# [1] "a and b"
knitr::combine_words(letters[1:3])
# [1] "a, b, and c"
knitr::combine_words(letters[1:4])
# [1] "a, b, c, and d"

这篇关于串函数,将以逗号分隔的单词向量与"and"连接.在最后一句话之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆