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语言编程
162
7
练习
考虑下面的代码段:
double test1() {
double a = 1.0 / 81;
double b = 0;
for (int i = 0; i < 729; ++ i)
b = b + a;
return b;
}
1.
test1() 函数保存为一个单独的文件。确保它正常运行。
2.
使用
R
写一个对应函数,比较
C++
R
版本的速度。
3.
double 型变量替换成 float 型,创建一个 test2() 函数。这样做后,还
能得到正确结果吗?
4.
b = b + a 改写成 b += a,从而使你的代码更像
c++
5.
(难度较大)i++ ++i 区别是什么
?
矩阵
每个向量类型有着对应的矩阵类型:NumericMatrixIntegerMatrix
CharacterMatrix LogicalMatrix。这些类型的使用方式与 NumericVector
的使用方式类似,主要的区别是:
当我们初始化时,我们需要指定行数和列数:
// 10 rows, 5 columns
NumericMatrix mat
10, 5
;
// Length 10
NumericVector v
10
;
使用
()
提取子集,例如,
mat(5, 4)
矩阵的第一个元素是
mat(0, 0)
,记住索引是从
0
而不是
1
开始。
我们使用
.nrow()
.ncol()
方法,确定行和列的数目。
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