1 paste的用法
paste(..., sep=" ", collapse=NULL)
- 本質(zhì)是把輸入的term轉(zhuǎn)變?yōu)閟tring,和
as.character意思一樣。然后進(jìn)行連接。- 每個term之間以
sep參數(shù)分隔開collapse是把結(jié)果進(jìn)行處理,也可以認(rèn)為怎么來對結(jié)果進(jìn)行折疊。
通過具體例子來看sep和collapse參數(shù)
> paste('Sample',1:10,sep = '')
[1] "Sample1" "Sample2" "Sample3" "Sample4" "Sample5" "Sample6"
[7] "Sample7" "Sample8" "Sample9" "Sample10"
> paste('Sample',1:10,sep = '_')
[1] "Sample_1" "Sample_2" "Sample_3" "Sample_4" "Sample_5" "Sample_6"
[7] "Sample_7" "Sample_8" "Sample_9" "Sample_10"
> paste('Sample',LETTERS[1:5],sep = '-')
[1] "Sample-A" "Sample-B" "Sample-C" "Sample-D" "Sample-E"
> paste(rep('Sample',5),LETTERS[1:10],sep = '-')#自動對應(yīng)補齊
[1] "Sample-A" "Sample-B" "Sample-C" "Sample-D" "Sample-E" "Sample-F"
[7] "Sample-G" "Sample-H" "Sample-I" "Sample-J"
> paste('Sample',letters[1:5],sep = '-', collapse = '~')
[1] "Sample-a~Sample-b~Sample-c~Sample-d~Sample-e"
再看一個官方例子
> (nth <- paste0(1:12, c("st", "nd", "rd", rep("th", 9))))
[1] "1st" "2nd" "3rd" "4th" "5th" "6th" "7th" "8th" "9th" "10th" "11th" "12th"
制表符分割
> paste('Sample',letters[1:5],sep = '-',collapse = '\t')
[1] "Sample-a\tSample-b\tSample-c\tSample-d\tSample-e"
#為了更好的看清楚結(jié)果,cat一下
> cat(paste('Sample',letters[1:5],sep = '-',collapse = '\t'))
Sample-a Sample-b Sample-c Sample-d Sample-e
#注意區(qū)分和上一條命令的區(qū)別,這里用`c連接
> cat(paste(c('Sample',letters[1:5]),sep = '-',collapse = '\t'))
Sample a b c d e
#換行
> cat(paste('Sample',letters[1:5],sep = '-',collapse = '\n'))
Sample-a
Sample-b
Sample-c
Sample-d
Sample-e
2 cat用法:輸出結(jié)果(直接輸出或?qū)懭胛募?/h1>
cat(... , file = "", sep = " ", fill = FALSE, labels = NULL,
append = FALSE)
cat(... , file = "", sep = " ", fill = FALSE, labels = NULL,
append = FALSE)
舉例
> cat('The content of hello.txt')
The content of hello.txt
> cat('The content of hello.txt',file = 'hello.txt')
第二條命令不會顯示任何信息,可以file.show查看
file.show('hello.txt')
截圖如下

cat也有sep參數(shù)
> cat('Sample',1:10, sep = '\n')
Sample
1
2
3
4
5
6
7
8
9
10
> cat('Sample',1:10, sep = '-')
Sample-1-2-3-4-5-6-7-8-9-10
注意以下兩個命令的區(qū)別
cat(paste(c('Sample',LETTERS[1]), collapse = '\t'))
cat(paste(c('Sample',LETTERS[1]), collapse = '\n'))
結(jié)果分別是
> cat(paste(c('Sample',LETTERS[1]), collapse = '\t'))
Sample A
> cat(paste(c('Sample',LETTERS[1]), collapse = '\n'))
Sample
A
3 sink的用法
sink(file = NULL, append = FALSE, type = c("output", "message"),
split = FALSE)
舉例
> sink('sink.txt')
> cat('The first line of sink.file\n')
> cat('The second line of sink file \n')
> sink()
> file.show('sink.txt')

sink