1、測試數據
root@PC1:/home/test# ls test.txt root@PC1:/home/test# cat test.txt 3 4 2 2 1 9 5 7 5 7 8 4 2 3 4 6
2、統計每行數據的最大值
root@PC1:/home/test# ls test.txt root@PC1:/home/test# cat test.txt 3 4 2 2 1 9 5 7 5 7 8 4 2 3 4 6 root@PC1:/home/test# awk '{for(i = 2; i <= NF; i++) {if($i < $(i - 1)) {$i = $(i - 1)}} {print $NF}}' test.txt ##輸出每行數據的最大值 4 9 8 6
3、輸出每行數據的最小值
root@PC1:/home/test# ls test.txt root@PC1:/home/test# cat test.txt 3 4 2 2 1 9 5 7 5 7 8 4 2 3 4 6 root@PC1:/home/test# awk '{for(i = 2; i <= NF; i++) {if($i > $(i - 1)) {$i = $(i - 1)}} {print $NF}}' test.txt ## 輸出每行數據中的最小值 2 1 4 2
4、R語言實現
list.files() dat <- read.table("test.txt", header = F) dat apply(dat, 1, max) apply(dat, 1, min)
> list.files() [1] "test.txt" > dat <- read.table("test.txt", header = F) ## 讀取測試數據 > dat V1 V2 V3 V4 1 3 4 2 2 2 1 9 5 7 3 5 7 8 4 4 2 3 4 6 > apply(dat, 1, max) ## 輸出每行數據的最大值 [1] 4 9 8 6 > apply(dat, 1, min) ## 輸出每行數據的最小值 [1] 2 1 4 2