用cat()和paste()串联字符串之间有什么区别? [英] What are the differences between concatenating strings with cat() and paste()?
问题描述
用cat
和paste
串联字符串之间有什么区别?
What are the differences between concatenating strings with cat
and paste
?
尤其是我有以下问题.
-
为什么R 不在打印调用
cat
的结果时使用双引号("
)(但是在使用paste
时使用引号)?>
Why does R not use the double quote (
"
) when it prints the results of callingcat
(but it uses quotes when usingpaste
)?
> cat("test")
test
> paste("test")
[1] "test"
为什么函数length
和mode
对R中的几乎所有对象都可用,而不是在cat
上起作用?
Why do the functions length
and mode
, which are functions available for almost all objects in R, not "work" on cat
?
> length(cat("test"))
test[1] 0
> mode(cat("test"))
test[1] "NULL"
为什么C风格的转义序列与cat
一起使用,而不能与paste
一起使用?
Why do C-style escape sequences work with cat
, but not with paste
?
> cat("1)Line1\n 2)Line2\n 3)Line3")
1)Line1
2)Line2
3)Line3
> paste("1)Line1\n 2)Line2\n 3)Line3")
[1] "1)Line1\n 2)Line2\n 3)Line3"
为什么R的回收规则不适用于cat
?
> cat("Grade", c(2, 3, 4, 5))
Grade 2 3 4 5
> paste("Grade", c(2, 3, 4, 5))
[1] "Grade 2" "Grade 3" "Grade 4" "Grade 5"
推荐答案
cat
和paste
用于非常不同的情况.
cat
and paste
are to be used in very different situations.
当您paste
某项内容且未将其分配给任何内容时,它将成为character
变量,该变量使用print.default
(character
的默认方法)进行print
编辑,因此使用引号等您可以查看print.default
的帮助,以了解如何修改输出的外观.
When you paste
something and don't assign it to anything, it becomes a character
variable that is print
-ed using print.default
, the default method for character
, hence the quotes, etc. You can look at the help for print.default
for understanding how to modify what the output looks like.
-
print.default
将不评估字符串中的转义字符,例如\n
.
print.default
will not evaluate escape characters such as\n
within a character string.
请查看此问题的答案,以了解如何从cat
捕获输出.
Look at the answers to this question for how to capture the output from cat
.
引用cat
(?cat
)的易读帮助
连接并打印
说明
输出对象,并连接表示形式.
cat
执行 转化率远低于
Concatenate and Print
Description
Outputs the objects, concatenating the representations.
cat
performs much less conversion than
...
cat
对于在用户定义的函数中生成输出很有用.它
将其参数转换为character
向量,并将它们连接为
单个character
向量,将给定的sep= string(s)
附加到每个
元素,然后将其输出.
cat
is useful for producing output in user-defined functions. It
converts its arguments to character
vectors, concatenates them to a
single character
vector, appends the given sep= string(s)
to each
element and then outputs them.
无(看不见的NULL
).
cat
不会返回任何内容,它只会输出到控制台或其他连接.
cat
will not return anything, it will just output to the console or another connection.
因此,如果您尝试运行length(cat('x'))
或mode(cat('x'))
,则说明您正在运行mode(NULL)
或length(NULL)
,它们将返回NULL
.
Thus, if you try to run length(cat('x'))
or mode(cat('x'))
, you are running mode(NULL)
or length(NULL)
, which will return NULL
.
粘贴帮助同样有用且具有描述性
The help for paste is equally helpful and descriptive
连接字符串
说明
转换为
character
后连接向量.
Concatenate Strings
Description
Concatenate vectors after converting to
character
.
....
连接值的character
向量.这将是长度
如果所有对象都是,则为零,除非塌陷为非NULL
它是一个空字符串.
A character
vector of the concatenated values. This will be of length
zero if all the objects are, unless collapse is non-NULL
in which case
it is a single empty string.
这篇关于用cat()和paste()串联字符串之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!