大數(shù)據(jù)的讀取

大數(shù)據(jù)的讀取

因?yàn)閺腦ena上下載的甲基化數(shù)據(jù)太大,在讀取時(shí)遇到點(diǎn)麻煩,特定查了下有沒(méi)有什么方法可以只讀取一部分,整理如下

大家有什么好的方法,敬請(qǐng)告知~

  • SCAN

    scan讀取.txt文件

    比如讀取下面文件的數(shù)據(jù),每個(gè)數(shù)據(jù)以空格分隔

    image

(1)scan("student.txt", what="c") #以字符串的格式讀取數(shù)據(jù)

(2)scan("student.txt", what="c", nlines=3) #讀取3行

(3)scan("student.txt", what="c", skip=1) #忽略第1行

(4)lst <- scan("student.txt", what = list(xh="", xm="", xb="", nl=0), skip=1) #讀取數(shù)據(jù)并保存到變量中

讀取結(jié)果如下

image
image

scan讀取.csv文件

.csv的文件默認(rèn)是逗號(hào)分隔,所以在讀取時(shí)要指定逗號(hào)為分隔符。

(1)scan("student.csv") #scan()函數(shù)默認(rèn)以double格式存儲(chǔ)數(shù)據(jù),而該文件中包含不能轉(zhuǎn)換的數(shù)據(jù)而報(bào)錯(cuò)

(2)scan("student.csv", what="c") #由于未指定分隔符,scan()函數(shù)將每一行作為一個(gè)數(shù)據(jù)域

(3)scan("student.csv", what="c", sep=",") #指定逗號(hào)作為分隔符后,可以正確讀取數(shù)據(jù)了

(4)scan("student.csv", what="c", sep=",", skip = 1) #忽略第一行的標(biāo)題行

(5)scan("student.csv", what = list(xh="", xm="", xb="", nl=0), sep=",", skip=1) #以列表的形式讀取數(shù)據(jù)

(6)m <- matrix(scan("student.csv", what="c", sep=",", skip=1), ncol=4, byrow=TRUE) #讀取數(shù)據(jù)并創(chuàng)建矩陣

image
  • fread

相比dplyr包,data.table包能夠更大程度地提高數(shù)據(jù)的處理速度。data.table,用于快速處理大數(shù)據(jù)集

fread讀取.tsv文件

  1. library(data.table)
  1. fread("example_data.txt",select=c("x1","x2"),data.table=F)
  1. data1<-fread("XXXXs.csv",header = T,stringsAsFactors = F)

readLines()

readLines()統(tǒng)計(jì)文件的行數(shù)

R語(yǔ)言逐行讀取文件:

con <- file(inFile, "r")
lineCnt = 0
while(1){
oneline = readLines(con, n = 1)
if(length(oneline) == 0){
break
}
lineCnt = lineCnt+1 ### 統(tǒng)計(jì)的文件行數(shù)
}
close(con)
?

REF:

scan:https://baijiahao.baidu.com/s?id=1609781727359063276

data.table包中的fread:https://www.cnblogs.com/nxld/p/6066797.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容