當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。