R0—New packages for reading data into R — fast

小伙伴儿们有福啦,2015年4月10日,Hadley Wickham大牛(开发了著名的ggplots包和plyr包等)和RStudio小组又出新作啦,新作品readr包和readxl包分别用于R读取text数据和Excel电子表格数据。事实上,R已经有了一堆读取数据的函数,比如read.table家族以及其巨多的变形,那么为了牛牛们为什么还要开发这两个包呢?原因很简单,这两个包的读取速度比R内置数据读入函数更快!!!记住哦,是快很多哈!不信,我们下来试试就知道啦!哈哈!平时读取小数据的童鞋可能不会有感觉,但读入的数据量比较大时,速度快就是一个很突出的优势啊,有木有?!废话不多说,上菜!
1)readr包示例
readr包提供了几个用R读取表格/文本数据的函数,并增添了额外的功能,而且更快!这在之间通常是用read.table家族函数来完成这些使命,现在可以轻松很多了啊!
首先,来看看readr包中第一个牛逼轰轰的函数read_table,它替换了之前read.table的功能,关键是更快,请记住,快、速度是这个包诞生的重要原因,可能是受大数据时代这股趋势的推动!我们来做一个实验!让这两个函数同时读取一个包含了4百万航数据的文件(数据地址:http://academic.udayton.edu/kissock/http/Weather/gsod95-current/NYNEWYOR.txt ),看看有什么有趣的发现!
Step1
看看数据格式,可以看到有四列,分别代表日,月,年和一个数值

Step2
打开R,运行以下命令,看看两个命令的运行时间!
> system.time(read_table(file = 'http://academic.udayton.edu/kissock/http/Weather/gsod95-current/NYNEWYOR.txt',col_names = c('DAY','MONTH','YEAR','TEMP')))
用户 系统 流逝
3.30 11.06 14.43
> system.time(read.table(file = 'http://academic.udayton.edu/kissock/http/Weather/gsod95-current/NYNEWYOR.txt',col.names = c('DAY','MONTH','YEAR','TEMP')))
用户 系统 流逝
1.92 1.62 96.10
这两个命令看起来类似,但是read.table函数大约花费了96.1秒完成,而read_table再不到15秒就完成啦(这可能是我这台破电脑的原因,官方的说法是:前者花了30秒左右,而后者不到一秒就搞定啦!!擦….这性能…无法比啊!)。也许有人会问,为什么会这样呢?原因在于:read_table函数把数据当成一个固定格式的稳健,底层使用C++快速的处理数据(与之对比的是,read.table支持列间任意数量的空格,而read_table要求每一列都排的很整齐,即一列中不能有"出头鸟")。但是,话是这么说,实际运用时,并没有这样严格的限制!
R基本包中有一个读取固定宽度数据集的函数,请看下面,再次见证readr包的神奇,对!!!就是这么神奇!!!
> system.time(dat <- read_fwf('http://academic.udayton.edu/kissock/http/Weather/gsod95-current/NYNEWYOR.txt',
+ fwf_widths(c(3,15,16,12),
+ col_names=c("DAY","MONTH","YEAR","TEMP"))))
用户 系统 流逝
0.67 1.70 2.40
> system.time(dat2 <- read.fwf('http://academic.udayton.edu/kissock/http/Weather/gsod95-current/NYNEWYOR.txt', c(3,15,16,12),
+ col.names=c("DAY","MONTH","YEAR","TEMP")))
用户 系统 流逝
0.73 0.49 89.03
看吧,这一对比,知道readr包的腻害了吧!
当然,上面只是readr包中一个简单的例子!readr中包括的其他函数还有:
readr::read_csv Read a delimited file into a data frame.
readr::read_file Read a file into a string.
readr::fwf_empty Read a fixed width file.
readr::read_lines Read lines from a file or string.
readr::read_log Read common/combined log file.
readr::read_table Read text file where columns are separated by whitespace.
2)readxl包示例
对于Excel格式的数据,对应了这里的readxl包,这个包提供了读取后缀为.xls和.xlsx格式的Excel表格。
需要注意地是,readxl包是托管在https://github.com/hadley/readxl 上的,因此,安装的时候安装地址要指定是github上的readxl库!
> library(devtools) #先安装这个包,可以快速的完成readxl包的安装!!!
> library(devtools)
> devtools::install_github("hadley/readxl")
目前,readxl包提供的函数只有read_excel,格式如下
Read_excel(spreadsheet, sheet=1, na,…. )
使用方法一看便知,这里就不再啰嗦啦!感兴趣的小伙伴儿赶紧去亲自探索吧!!!

R0—New packages for reading data into R — fast的更多相关文章
- Importing data in R 1
目录 Importing data in R 学习笔记1 flat files:CSV txt文件 packages:readr read_csv() read_tsv read_delim() da ...
- 【MySQL】MySQL同步报错-> Last_IO_Error: Got fatal error 1236 from master when reading data from binary log
这个报错网上搜索了一下,大部分是由于MySQL意外关闭或强制重启造成的binlog文件事务点读取异常造成的主从同步报错 Last_IO_Error: Got fatal error 1236 from ...
- mysql 主从 Got fatal error 1236 from master when reading data from binary log: 'Could not find first 错误
本地MySQL环境,是两台MySQL做M-M复制.今天发现错误信息: mysql 5.5.28-log> show slave status\G ************************ ...
- Last_IO_Errno: 1236 Last_IO_Error: Got fatal error 1236 from master when reading data from binary lo
mysql> show slave status\G *************************** 1. row *************************** ...
- SQL data reader reading data performance test
/*Author: Jiangong SUN*/ As I've manipulated a lot of data using SQL data reader in recent project. ...
- OpenTSDB-Querying or Reading Data
Querying or Reading Data OpenTSDB offers a number of means to extract data such as CLI tools, an HTT ...
- Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'系列一:
从库报这个错误:Got fatal error 1236 from master when reading data from binary log: 'Could not find first lo ...
- mysql从库Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'报错处理
年后回来查看mysql运行状况与备份情况,登录mysql从库查看主从同步状态 mysql> show slave status\G; *************************** . ...
- (转) 6 ways of mean-centering data in R
6 ways of mean-centering data in R 怎么scale我们的数据? 还是要看我们自己数据的特征. 如何找到我们数据的中心? Cluster analysis with K ...
随机推荐
- SSH 框架的心得
使用SSH框架做完了一个普通网站的前后台项目,成热写点心得,免得以后再入坑.其中使用 Strust2 2.3.33 + Spring 4.3.9 + Hibernate 5.2.10 eclipse ...
- Matlab中TCP通讯-实现外部程序提供优化目标函数解
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Matlab中TCP通讯-实现外部程序提供优化目标函数解 本文地址:http://te ...
- 转载-C++ vector 用法
转自:http://www.cnblogs.com/wang7/archive/2012/04/27/2474138.html 在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. ...
- HDU2460-Network
题目 给一个\(n\)个点\(m\)条边的无向连通图,\(Q\)次往图中加边,每次加边后问图中的桥有多少个.(加边后边留着). \(n\le 10^5,m\le 2\times 10^5,Q\le 1 ...
- JAVA中的堆、栈等内存分析
在 JAVA 中,有六个不同的地方可以存储数据 1. 寄存器( register ) 这是最快的存储区,因为它位于不同于其他存储区的地方——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据 ...
- 【NuGet】使用NuGet打包并发布至ProGet过程 (打包再次详解)【下篇】
一.前言 上篇[1]主要介绍了利用csproj文件使用NuGet打包至ProGet的过程,并附上了用于在Jenkins上运行的python脚本.本篇的主要内容分为以下几点: 1. Nuspec与Nup ...
- 【BZOJ1493】【NOI2007】项链工厂(线段树)
[BZOJ1493]项链工厂(线段树) 题面 BZOJ 洛谷 Description T公司是一家专门生产彩色珠子项链的公司,其生产的项链设计新颖.款式多样.价格适中,广受青年人的喜爱. 最近T公司打 ...
- Codeforces Round #431
我太菜啦 A 一道斯波题,我想了一会儿后写了dp,其实if就好了 B做法很一眼,但有一些细节,分类一下就好了 C一直在想dp,挂机30分钟,后来dp来模拟分层图状态扩展的过程 D不会 然后发现room ...
- 【Learning】一步步地解释Link-cut Tree
简介 Link-cut Tree,简称LCT. 干什么的?它是树链剖分的升级版,可以看做是动态的树剖. 树剖专攻静态树问题:LCT专攻动态树问题,因为此时的树剖面对动态树问题已经无能为力了(动态树问题 ...
- Linux必知必会——xargs命令
1.功能: xargs可以将stdin中以空格或换行符进行分隔的数据,形成以空格分隔的参数(arguments),传递给其他命令.因为以空格作为分隔符,所以有一些文件名或者其他意义的名词内含有空格的时 ...