温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

R语言读取大数据

发布时间:2020-07-20 22:19:27 来源:网络 阅读:3169 作者:qizok 栏目:大数据

普通情况下使用scan读取数据

x <- scan("D:\\test.txt")

按列读入,指定数据类型

x <- scan("test2dat.txt", what=list("",0,0))  #读取三列数据,第一列是字符,第二和第三列是数值
#以下写法也可以
x2 <- scan("test2dat.txt", list(name="", num1=0,num2=0)) # 每个list都有个名字,分别为name,num1,num2

可以指定读取的行数,以下为读取以逗号分割的csv文件的读取方法

mydata <- read.table("test_nrow.txt.txt",sep=",", header=TRUE,nrow=5)  #读取除了表头之外的5行数据

参考: http://www.biostat.jhsph.edu/~rpeng/docs/R-large-tables.html

tab5rows <- read.table("datatable.txt", header = TRUE, nrows = 5)
classes <- sapply(tab5rows, class)
tabAll <- read.table("datatable.txt", header = TRUE, colClasses = classes)


也可以用data.table 读取大数据

install.packages("data.table")
library(data.table)
mydata <- fread("test.table.txt")    #读取文件时会显示 Read **.*% of ***** rows, 读取完毕会有提示
#查看文件的前6行
head(mydata)

参考:http://www.r-bloggers.com/reading-large-data-tables-in-r/


也可以使用ff包

setwd("D:/data test")
library(ff)
ffdf1 <- read.table.ffdf(file = "test.ido", header = TRUE,  sep = "|")

参考: http://stackoverflow.com/questions/11782084/reading-in-large-text-files-in-r

http://www.bytemining.com/wp-content/uploads/2010/08/r_hpc_II.pdf


使用Python打开大数据的话,采用mmap

参考: http://stackoverflow.com/questions/11159077/python-load-2gb-of-text-file-to-memory

http://davetang.org/muse/2013/09/03/handling-big-data-in-r/


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI