Why does R not use the double quote (") when it prints the results of calling cat (but it uses quotes when using paste)?> 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执行 转化率远低于print. Concatenate and Print Description Outputs the objects, concatenating the representations. cat performs much less conversion than print. ... 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()串联字符串之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 23:33