用cat()和paste()串联字符串之间有什么区别? [英] What are the differences between concatenating strings with cat() and paste()?

查看:167
本文介绍了用cat()和paste()串联字符串之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

catpaste串联字符串之间有什么区别?

What are the differences between concatenating strings with cat and paste?

尤其是我有以下问题.

  1. 为什么R 在打印调用cat的结果时使用双引号(")(但是在使用paste时使用引号)?

  1. 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"

  • 为什么函数lengthmode对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"
    

  • 推荐答案

    catpaste用于非常不同的情况.

    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()串联字符串之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆