Skip to Content
R 语言经典实例(原书第 2 版)
book

R 语言经典实例(原书第 2 版)

by J.D. Long, Paul Teetor
June 2020
Beginner to intermediate
522 pages
9h 6m
Chinese
China Machine Press
Content preview from R 语言经典实例(原书第 2 版)
数据结构
133
append(1:10, 99, after = 0)
#> [1] 99 1 2 3 4 5 6 7 8 9 10
问题 5.1 节中的注释也适用于此处。如果要将单个数据项插入向量,你可能在元素水平
工作;而在向量水平工作更容易编码,并且运行更快。
5.3 理解循环规则
5.3.1 问题
你想要理解奇妙的循环规则,它知道 R 软件如何处理不等长度的向量。
5.3.2 讨论
当作向量运算时,R 软件执行元素对元素的运算。当两个向量具有相同的长度时,这很
有效:R 软件成对搭配这些向量的元素,并将操作应用于这些元素对。
但是,当向量长度不等时会发生什么?
在这种情况下,R 软件应用循环规则。R 从两个向量的第一个元素开始,成对处理向量
元素。在某个元素位置,较短的向量已经处理完所有的元素,而较长的向量仍然具有未
处理的元素。这时候,R 软件返回到较短向量的开始位置,“循环”应用它的元素,同时
继续从较长向量中处理元素,直到这个操作完成为止。它将根据需要来循环较短向量的
元素,直到操作完成。
可以进行如下的循环规则的可视化。下面是两个向量的图表,1
6 的整数和 1
3 的整数:
1:6 1:3
----- -----
1 1
2 2
3 3
4
5
6
显然,向量 1
6 比向量 1
3 长。如果使用 (1
6)+(1
3)添加向量,看上去向量
1
3 的元素太少。然而,R 软件循环应用向量 1
3 的元素,将这两个向量配对并产生
一个有 6 个元素的向量:
1:6 1:3 (1:6) + (1:3) ...
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

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

Aurélien Géron
大规模数据分析和建模:基于 Spark 与 R

大规模数据分析和建模:基于 Spark 与 R

Javier Luraschi, Kevin Kuo, Edgar Ruiz
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey

Publisher Resources

ISBN: 9787111656814