批量读入一个文件夹中文件的数据操作实例
批量处理
> 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,很有用滴。