大數(shù)據(jù)的讀取
因為從Xena上下載的甲基化數(shù)據(jù)太大,在讀取時遇到點麻煩,特定查了下有沒有什么方法可以只讀取一部分婉商,整理如下
大家有什么好的方法,敬請告知~
-
SCAN
scan讀取.txt文件
比如讀取下面文件的數(shù)據(jù)渣叛,每個數(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ù)并保存到變量中
讀取結果如下:
scan讀取.csv文件
.csv的文件默認是逗號分隔
丈秩,所以在讀取時要指定逗號為分隔符。
(1)scan("student.csv") #scan()函數(shù)默認以double格式存儲數(shù)據(jù)淳衙,而該文件中包含不能轉換的數(shù)據(jù)而報錯
(2)scan("student.csv", what="c") #由于未指定分隔符蘑秽,scan()函數(shù)將每一行作為一個數(shù)據(jù)域
(3)scan("student.csv", what="c", sep=",") #指定逗號作為分隔符后饺著,可以正確讀取數(shù)據(jù)了
(4)scan("student.csv", what="c", sep=",", skip = 1) #忽略第一行的標題行
(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)建矩陣
-
fread
相比dplyr包,data.table包能夠更大程度地提高數(shù)據(jù)的處理速度肠牲。data.table,用于快速處理大數(shù)據(jù)集
fread讀取.tsv文件
- library(data.table)
- fread("example_data.txt",select=c("x1","x2"),data.table=F)
- data1<-fread("XXXXs.csv",header = T,stringsAsFactors = F)
readLines()
readLines()統(tǒng)計文件的行數(shù)
R語言逐行讀取文件:
con <- file(inFile, "r")
lineCnt = 0
while(1){
oneline = readLines(con, n = 1)
if(length(oneline) == 0){
break
}
lineCnt = lineCnt+1 ### 統(tǒng)計的文件行數(shù)
}
close(con)
?
REF:
scan:https://baijiahao.baidu.com/s?id=1609781727359063276
data.table包中的fread:https://www.cnblogs.com/nxld/p/6066797.html