You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将R语言向量多值拼接为单句字符串?glue包问题求助

要解决这个问题,核心是先把tables向量的元素合并成单个用逗号分隔的字符串,再传入glue,这样就不会循环生成多行输出了。

给你两种常用的实现方式:

方式1:使用基础R的paste()函数

library(glue)
tables = c("Brif", "Amni","Calp")
glue("The correct tables include: {paste(tables, collapse = ', ')}")

方式2:使用stringr包的str_c()函数(更简洁)

如果已经在用tidyverse工具链,str_c()是更推荐的字符串合并函数:

library(glue)
library(stringr)
tables = c("Brif", "Amni","Calp")
glue("The correct tables include: {str_c(tables, collapse = ', ')}")

两种方式运行后都会输出预期的单句:

The correct tables include: Brif, Amni, Calp

内容的提问来源于stack exchange,提问作者Rara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 13:14:58