[英]How can I print all the elements of a vector in a single string in R?
Sometimes I want to print all of the elements in a vector in a single string, but it still prints the elements separately: 有时我想在一个字符串中打印矢量中的所有元素,但它仍然分别打印元素:
notes <- c("do","re","mi")
print(paste("The first three notes are: ", notes,sep="\t"))
Which gives: 这使:
[1] "The first three notes are: \tdo" "The first three notes are: \tre"
[3] "The first three notes are: \tmi"
What I really want is: 我真正想要的是:
The first three notes are: do re mi
The simplest way might be to combine your message and data using one c
function: 最简单的方法可能是使用一个
c
函数组合您的消息和数据:
paste(c("The first three notes are: ", notes), collapse=" ")
### [1] "The first three notes are: do re mi"
The cat
function both con cat enates the elements of the vector and prints them: 该
cat
的功能都CON 猫 enates向量的元素,并将它们打印:
cat("The first three notes are: ", notes,"\n",sep="\t")
Which gives: 这使:
The first three notes are: do re mi
The sep
argument allows you to specific a separating character (eg here \\t
for tab). sep
参数允许您指定一个分隔字符(例如,此处\\t
为tab)。 Also, Adding a newline character (ie \\n
) at the end is also recommended if you have any other output or a command prompt afterwards. 此外,如果您之后有任何其他输出或命令提示符,也建议在末尾添加换行符(即
\\n
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.