简体   繁体   English

如何在R中的单个字符串中打印矢量的所有元素?

[英]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.

相关问题 R:如何自动将向量的所有元素插入到重复函数中? - R: How can I automatically insert all elements of an vector into a repeated function? 如何从使用管道分隔 R 中向量元素的字符串中解析出文本? - How can I parse out text from a string that uses pipes to separate elements of a vector in R? 如何删除 R 中向量的所有无价值元素? - How do I remove all valueless elements of a vector in R? 如何排除向量中某个数字以下的所有元素,同时删除R中另一个向量中的相应元素? - How can I exclude all the elements below a number in a vector, and at the same time remove the corresponding element in the other vector in R? 我们可以合并 R 中向量的所有元素吗? - Can we merge all the elements of a vector in R? 在 R 中,如何使用返回可能输出向量的歧义的所有选项替换字符串中的歧义字符? - In R, How can I replace ambiguous characters in string with all options for the ambiguity returning a vector of possible outputs? 单行打印向量元素 - Print elements of vector in single line 如何以可以在R中创建另一个矢量的方式打印字符矢量? - how can I print a character vector in a way I can create another vector in R? 如何将向量中的元素作为 R 中的参数传递给 function? - how can I pass elements in a vector to a function as parameters in R? 如何在R中的字符向量中随机选择元素? - How can I randomly choosing elements in character vector in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM