Skip to Content
高效R语言编程
book

高效R语言编程

by Colin Gillespie, Robin Lovelace
August 2018
Intermediate to advanced
227 pages
4h 16m
Chinese
China Electric Power Press Ltd.
Content preview from 高效R语言编程
150
7
2.
查看下面的函数定义,说下你对整数用法的理解:
tail.matrix()
lm()
3.
创建一个整数矩阵与数值矩阵,使用 pryr::object_size() 比较两个矩阵。
4.
用于
tail.matrix() seq.int() 函数与标准 seq() 函数有何差异?
一个相对节约内存空间的思想是使用
bit
包的向量替换逻辑向量,仅占用
1/300
的空间(但你不能使用
NAs
)。
例子:优化
move_square()
函数
7-2
展示了在模拟
Monopoly
游戏中,我们的主要瓶颈是 move_squre() 函数。
在这个函数中,我们花费了大约
50%
的时间创建一个数据框,
20%
的时间计
算行的和,剩余的是比较操作。这段代码可通过下列方法非常简单地进行优
化(同时保持相同的整体结构)
2
使用
1:6
替换了原有的
seq(1,6)
生成骰子的
6
个可能值。另外,使用矩阵
替换数据集,并调用一次
sample()
函数。
matrix
sample
1:6, 6, replace = TRUE
, ncol = 2
总体来说,修改后的这行代码大约有
25
倍的速度提升;速度提升主要来
自转换成矩阵。
使用
rowSums()
替换了
apply()
apply()
函数调用已经是很快的,不过我
们从数据集切换到矩阵。使用矩阵的
rowSums()
带来了
10
倍的速度提升。
if
条件语句中使用
&&
,这大约是
&
的两倍速度。
重构后代码居然比原来版本速度提升了
20
倍,吃惊不?比较图
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

数据科学之编程技术:使用R进行数据清理、分析与可视化

数据科学之编程技术:使用R进行数据清理、分析与可视化

迈克尔 弗里曼, 乔尔 罗斯
R数据科学

R数据科学

Hadley Wickham, Garrett Grolemund

Publisher Resources

ISBN: 9787519820855