当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R语言 cat()用法及代码示例


R 语言中的 cat() 函数用于打印输出到屏幕或文件。

用法: 
cat(…, file = “”, sep = ” “, fill = FALSE, labels = NULL, append = FALSE)
参数: 
…: atomic vectors, names, NULL and objects with no output 
file: the file in which printing will be done 
sep: specified separator 
fill: If fill=TRUE, a new line will be printed, otherwise not 
labels: specified labels 
 

范例1:

Python3


# R program to illustrate
# cat function
# Creating some string and print it
x <- "GeeksforGeeks\n"
y <- "Geeks\n"
# Calling cat() function
cat(x)
cat(y)
# Creating a sequence from 1 to 9
x <- 1:9
# Calling cat() function
cat(x, sep =" + ")
cat("\n")
cat(x, sep =" / ")

输出:



GeeksforGeeks
Geeks
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9

范例2:

Python3


# R program to illustrate
# cat function
# Creating a sequence from 1 to 9
x <- 1:9
# Calling cat() function
# fill value TRUE will print
# a new line
cat(x, sep =" + ", fill = TRUE)
cat(x, sep =" / ", fill = FALSE)
# Printing new line
cat("\n")
# Each number from 1 to 9 will be
# assigned with alphabets a to i
cat(x, fill = 2, labels = paste("(", letters[1:9], "):"))        

输出:

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9
( a ):1 
( b ):2 
( c ):3 
( d ):4 
( e ):5 
( f ):6 
( g ):7 
( h ):8 
( i ):9



相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Printing out to the Screen or to a File in R Programming – cat() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。