批量读入一个文件夹中文件的数据操作实例
批量處理
> coo = dir("test")
> path = "/panfs/TC_FUN/USER/group3/yanzengli/other/study/R_study/knowledge/test"
> doc_path = sapply(coo, function(names) paste(path, names, sep='/'))
>doc <- sapply( doc_path, function(doc) readLines(doc) )
詳解:
dir 函數作用:把所有目錄下的文件名裝入一個數組,效果如下:
> coo
[1] "co_1.txt" "co_2.txt" "co_3.txt"
paste函數作用:把一串字符連成一個,在這兒是把連接成完整的路徑,效果如下:
> doc_path
co_1.txt
"/panfs/TC_FUN/USER/group3/yanzengli/other/study/R_study/knowledge/test/co_1.txt"
co_2.txt
"/panfs/TC_FUN/USER/group3/yanzengli/other/study/R_study/knowledge/test/co_2.txt"
co_3.txt
"/panfs/TC_FUN/USER/group3/yanzengli/other/study/R_study/knowledge/test/co_3.txt"
sapply函數作用:相當于perl中的foreach語句,循環使用某變量中元素,然后返回所有的內容。
但是有個缺點,當存在多個文件的時候,會并排所有的結果,這個是個致命的缺點,不過反正已經
讀入數據了,后面可以用變量來處理,畢竟是已經把所有的數據都已經讀入一個矩陣,效果如下:
> doc
????? co_1.txt? co_2.txt? co_3.txt
?[1,] "1\t657"? "31\t609" "61\t834"
?[2,] "2\t593"? "32\t584" "62\t724"
?[3,] "3\t550"? "33\t589" "63\t785"
?[4,] "4\t751"? "34\t532" "64\t812"
?[5,] "5\t647"? "35\t529" "65\t643"
?[6,] "6\t492"? "36\t507" "66\t846"
?[7,] "7\t595"? "37\t973" "67\t883"
?[8,] "8\t430"? "38\t587" "68\t836"
?[9,] "9\t533"? "39\t365" "69\t723"
[10,] "10\t398" "40\t690" "70\t693"
[11,] "11\t541" "41\t575" "71\t925"
[12,] "12\t489" "42\t496" "72\t823"
[13,] "13\t632" "43\t529" "73\t880"
[14,] "14\t530" "44\t805" "74\t813"
[15,] "15\t705" "45\t971" "75\t786"
[16,] "16\t730" "46\t900" "76\t801"
[17,] "17\t537" "47\t873" "77\t891"
[18,] "18\t566" "48\t773" "78\t762"
[19,] "19\t610" "49\t865" "79\t777"
[20,] "20\t458" "50\t710" "80\t866"
[21,] "21\t424" "51\t734" "81\t684"
[22,] "22\t406" "52\t980" "82\t855"
[23,] "23\t630" "53\t660" "83\t880"
[24,] "24\t698" "54\t844" "84\t908"
[25,] "25\t520" "55\t907" "85\t746"
[26,] "26\t520" "56\t657" "86\t846"
[27,] "27\t587" "57\t676" "87\t951"
[28,] "28\t538" "58\t796" "88\t815"
[29,] "29\t411" "59\t712" "89\t544"
[30,] "30\t483" "60\t769" "90\t794"
好了,先這樣吧,后面還有一個介紹怎么把圖片輸出到pdf或者svg,很有用滴。
轉載于:https://www.cnblogs.com/bangemantou/archive/2013/01/29/2881184.html
總結
以上是生活随笔為你收集整理的批量读入一个文件夹中文件的数据操作实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数论学习准备
- 下一篇: 怎么更换WIN7欢迎界面的背景图?