What are the differences between concatenating strings with cat
and paste
?
用cat和paste连接字符串的区别是什么?
In particular, I have the following questions.
特别是,我有以下几个问题。
-
Why does R not use the double quote (
"
) when it prints the results of callingcat
(but it uses quotes when usingpaste
)?为什么R在打印调用cat的结果时不使用双引号(")(但在使用粘贴时使用引号)?
> cat("test")test> paste("test")[1] "test"
-
Why do the functions
length
andmode
, which are functions available for almost all objects in R, not "work" oncat
?为什么函数长度和模式(在R中几乎对所有对象都可用)不能在cat上“工作”?
> length(cat("test"))test[1] 0> mode(cat("test"))test[1] "NULL"
-
Why do C-style escape sequences work with
cat
, but not withpaste
?为什么C-style转义序列可以用cat,而不能用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"
-
Why doesn't R's recycling rule work with
cat
?为什么R的回收规则不能适用于猫?
> 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"
1 个解决方案
#1
15
cat
and paste
are to be used in very different situations.
cat和paste是在非常不同的情况下使用的。
paste
is not print
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、字符的默认方法、引号等进行打印。
-
print.default
will not evaluate escape characters such as\n
within a character string. - 默认情况下,不会在字符串中计算转义字符,例如\n。
Look at the answers to this question for how to capture the output from cat
.
查看这个问题的答案,了解如何捕获cat的输出。
Quoting from the easy to read help for cat
(?cat
)
引用易读的帮助猫(?猫)
Concatenate and Print
Description
Outputs the objects, concatenating the representations.
cat
performs much less conversion than输出对象,连接表示。cat执行的转换比打印要少得多。
...
…
Details
cat
is useful for producing output in user-defined functions. It converts its arguments tocharacter
vectors, concatenates them to a singlecharacter
vector, appends the givensep= string(s)
to each element and then outputs them.cat用于在用户定义函数中生成输出。它将参数转换为字符向量,将它们连接到单个字符向量,将给定的sep= string(s)附加到每个元素,然后输出它们。
Value
None (invisible
NULL
).(看不见的NULL)。
cat
will not return anything, it will just output to the console or another connection.
cat不会返回任何内容,它只会输出到控制台或其他连接。
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
.
因此,如果您尝试运行length(cat('x'))或mode(cat('x')),那么您正在运行mode(NULL)或length(NULL),它将返回NULL。
The help for paste is equally helpful and descriptive
粘贴的帮助同样有帮助和描述性
Concatenate Strings
Description
Concatenate vectors after converting to
character
.转换为字符后连接向量。
....
....
Value
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.连接值的字符向量。如果所有对象都是,那么它的长度将为0,除非折叠是非空的,在这种情况下,它是一个空字符串。
#1
15
cat
and paste
are to be used in very different situations.
cat和paste是在非常不同的情况下使用的。
paste
is not print
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、字符的默认方法、引号等进行打印。
-
print.default
will not evaluate escape characters such as\n
within a character string. - 默认情况下,不会在字符串中计算转义字符,例如\n。
Look at the answers to this question for how to capture the output from cat
.
查看这个问题的答案,了解如何捕获cat的输出。
Quoting from the easy to read help for cat
(?cat
)
引用易读的帮助猫(?猫)
Concatenate and Print
Description
Outputs the objects, concatenating the representations.
cat
performs much less conversion than输出对象,连接表示。cat执行的转换比打印要少得多。
...
…
Details
cat
is useful for producing output in user-defined functions. It converts its arguments tocharacter
vectors, concatenates them to a singlecharacter
vector, appends the givensep= string(s)
to each element and then outputs them.cat用于在用户定义函数中生成输出。它将参数转换为字符向量,将它们连接到单个字符向量,将给定的sep= string(s)附加到每个元素,然后输出它们。
Value
None (invisible
NULL
).(看不见的NULL)。
cat
will not return anything, it will just output to the console or another connection.
cat不会返回任何内容,它只会输出到控制台或其他连接。
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
.
因此,如果您尝试运行length(cat('x'))或mode(cat('x')),那么您正在运行mode(NULL)或length(NULL),它将返回NULL。
The help for paste is equally helpful and descriptive
粘贴的帮助同样有帮助和描述性
Concatenate Strings
Description
Concatenate vectors after converting to
character
.转换为字符后连接向量。
....
....
Value
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.连接值的字符向量。如果所有对象都是,那么它的长度将为0,除非折叠是非空的,在这种情况下,它是一个空字符串。