Chr*_*oms 7 r string-concatenation
有时我想在一个字符串中打印矢量中的所有元素,但它仍然分别打印元素:
notes <- c("do","re","mi")
print(paste("The first three notes are: ", notes,sep="\t"))
Run Code Online (Sandbox Code Playgroud)
这使:
[1] "The first three notes are: \tdo" "The first three notes are: \tre"
[3] "The first three notes are: \tmi"
Run Code Online (Sandbox Code Playgroud)
我真正想要的是:
The first three notes are: do re mi
Run Code Online (Sandbox Code Playgroud)
最简单的方法可能是使用一个函数组合您的消息和数据c:
paste(c("The first three notes are: ", notes), collapse=" ")
### [1] "The first three notes are: do re mi"
Run Code Online (Sandbox Code Playgroud)
该cat功能既CON猫enates向量的元素,并将它们打印:
cat("The first three notes are: ", notes,"\n",sep="\t")
Run Code Online (Sandbox Code Playgroud)
这使:
The first three notes are: do re mi
Run Code Online (Sandbox Code Playgroud)
该sep参数允许您指定一个分隔字符(例如此处\t用于制表符)。此外,\n如果您之后有任何其他输出或命令提示符,也建议在末尾添加换行符(即)。