R语言 打印输出

R语言 打印输出

在R程序中,有各种方法来打印输出。最常见的方法是在R程序中打印输出,有一个叫做 print() 的函数被使用。此外,如果R程序被逐行写入 控制台 ,那么输出就会被正常打印,不需要使用任何函数来打印输出。要做到这一点,只需选择输出变量并按下 运行 按钮。 例如

# select 'x' and then press 'run' button
# it will print 'GeeksforGeeks' on the console
x <- "GeeksforGeeks"
x
R

输出

[1] "GeeksforGeeks"
R

打印R程序的输出

使用print()函数打印输出

使用 print() 函数打印输出是R语言中最常见的方法,这个方法的实现非常简单。

语法: print(“任何字符串”) 或者,print(变量)

例子

# R program to illustrate
# printing output of an R program
 
# print string
print("GFG")
 
# print variable
# it will print 'GeeksforGeeks' on the console
x <- "GeeksforGeeks"
print(x)
R

输出

[1] "GFG"
[1] "GeeksforGeeks"
R

在print()函数中使用paste()函数打印输出

R提供了一个 粘贴() 方法来打印带有字符串和变量的输出。这个方法定义在 print() 函数内。 paste() 将其参数转换为字符串。人们也可以使用 paste0() 方法。

注意: paste()和paste0()的区别在于,参数sep默认为””(paste)和””(paste0)。

语法: print(paste(“any string”, variable)) or, print(paste0(variable, “any string”)

例子

# R program to illustrate
# printing output of an R program
 
x <- "GeeksforGeeks"
 
# using paste inside print()
print(paste(x, "is best (paste inside print())"))
 
# using paste0 inside print()
print(paste0(x, "is best (paste0 inside print())"))
R

输出

[1] "GeeksforGeeks is best (paste inside print())"
[1] "GeeksforGeeksis best (paste0 inside print())"
R

使用sprintf()函数打印输出

sprintf() 基本上是一个 C语言库函数 。这个函数是用来像 C语言 一样打印字符串的。该函数作为一个封装函数,可以像 C语言 一样将数值和字符串一起打印出来。该函数返回一个包含要打印的字符串和变量的格式化组合的字符向量。

语法: sprintf(“any string %d”, variable) or, sprintf(“any string %s”, variable) or, sprintf(“any string %f”, variable)) 等。

例子

# R program to illustrate
# printing output of an R program
 
x = "GeeksforGeeks" # string
x1 = 255            # integer
x2 = 23.14          # float
 
# string print
sprintf("%s is best", x)
 
# integer print
sprintf("%d is integer", x1)
 
# float print
sprintf("%f is float", x2)
R

输出

> sprintf("%s is best", x)
[1] "GeeksforGeeks is best"
> sprintf("%d is integer", x1)
[1] "255 is integer"
> sprintf("%f is float", x2)
[1] "23.140000 is float"
R

使用cat()函数打印输出

另一种在R中打印输出的方法是使用cat()函数。它与 print() 函数相同。 cat() 将其参数转换为字符串。这对于打印用户定义的函数中的输出很有用。

语法: cat(“任何字符串”) 或者,cat(“任何字符串”,变量)

例子

# R program to illustrate
# printing output of an R program
 
# print string with variable
# "\n" for new line
x = "GeeksforGeeks"
cat(x, "is best\n")
 
# print normal string
cat("This is R language")
R

输出

GeeksforGeeks is best
This is R language
R

使用message()函数打印输出

另一种在R中打印东西的方法是使用 message() 函数。这不是用于打印输出,而是用于显示程序中没有警告或错误的简单诊断信息。但它可以用于打印输出的正常用途。

语法: message(“any string”) or, message(“any string”, variable)

例子

# R program to illustrate
# printing output of an R program
 
x = "GeeksforGeeks"
# print string with variable
message(x, "is best")
 
# print normal string
message("This is R language")
R

输出

GeeksforGeeks is best
This is R language 
R

将输出写到一个文件中

为了将一个变量的值打印或写入一个文件,有一个函数叫做 write()。 这个函数使用一个叫做 table 的选项来写一个文件。

语法: write.table(variable, file = “file1.txt”) 或者,write.table(“any string”, file = “file1.txt”)

例子

# R program to illustrate
# printing output of an R program
 
x = "GeeksforGeeks"
# write variable
write.table(x, file = "my_data1.txt")
 
# write normal string
write.table("GFG is best", file = "my_data2.txt")
R

输出:

R语言 打印输出

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册