简体   繁体   English

R中的print()和print(paste())函数有什么区别?

[英]What's the difference between the print() and the print(paste())-functions in R?

As stated in the title. 如标题中所述。

I'm working myself through the Datacamp R tutorials and I find the instructions to be....lacking. 我正在研究Datacamp R教程,发现其中的说明是...。

One of the more annoying oversights is that many of the basic functions are never defined or explained, among them the print() and paste()-functions. 较烦人的疏忽之一是,许多基本功能从未定义或解释过,其中包括print()和paste()函数。 Some of the time I can use one and some of the time the other, right now this seems totally random to me. 有时候我可以使用一个,而有时候可以使用另一个,现在对我来说这似乎是完全随机的。

I have scoured the internet in search of a clear-cut answer but come up short. 我搜寻了互联网,以寻找明确的答案,但是很简短。

I will restate my question: 我将重申我的问题:

When can I use the print()-function and when do I have to insert a paste-function in the parentheis, print(paste())? 什么时候可以使用print()函数,什么时候必须在括号中插入粘贴函数print(paste())?

print 打印

If you are at the R console then the result of any expression you type in is automatically printed so you don't need to specify print so these are the same: 如果您在R控制台上,则键入的任何表达式的结果都会自动打印出来,因此您无需指定print因此它们是相同的:

# same when typed into the R console

32
## [1] 32

print(32)
## [1] 32

however, automatic printing is not done in R scripts, in R functions or in any context where it is within the body of some larger expression such as within a for or while loop. 但是,自动打印不会在R脚本,R函数中或在较大表达式的主体内(例如forwhile循环内)的任何上下文中for Thus, to have 32 print from within a function use print . 因此,要在一个函数中具有32打印,请使用print In these cases nothing would have been printed had we not used print . 在这些情况下,如果不使用print什么也不会print

f <- function() {
  print(32)
}
x <- f()
## [1] 32

for(i in 1:3) print(32)
## [1] 32
## [1] 32
## [1] 32

Note that print prints out a single object. 请注意, print打印出单个对象。 If you want to print out several objects you can either use several print statements or else combine the objects into a single larger object. 如果要打印出多个对象,则可以使用多个打印语句,也可以将这些对象组合为一个较大的对象。 For example, 例如,

# print A and then print B
"A"
## [1] "A"
"B"
## [1] "B"

paste("A", "B", sep = ",")  # create single char string and print it
## [1] "A,B"

c("A", "B")  # create vector of two elements and print it out
## [1] "A" "B"

There is also cat . 还有cat

x <- "A"
y <- "B"
cat("x:", x, "y:", y, "\n")
## x: A y: B 

paste

paste has nothing to do with printing. paste与打印无关。 Its function is to take its arguments and create a character string out of them so paste("A", "B") creates the character string "AB" . 它的功能是获取参数并从中创建字符串,因此paste("A", "B")创建字符串"AB" Of course if you enter a paste command at the R console since R prints out the value of any expression typed into it, the result of the paste will be printed out. 当然,如果您在R控制台上输入paste命令,因为R会打印出键入到其中的任何表达式的值,所以粘贴的结果将被打印出来。 Here are some examples of automatic printing assuming that these expressions are typed into the R console. 这是一些自动打印的示例,假定这些表达式在R控制台中键入。

# assume these are typed into the R console

paste("A", "B")  # R automatically prints result of paste
## [1] "A B"

paste(32)  # 32 is converted to character; then R automatically prints it
## [1] "32"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM