Book Image

R High Performance Programming

Book Image

R High Performance Programming

Overview of this book

Table of Contents (17 chapters)
R High Performance Programming
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Vectorization


Most R users should have encountered this first tweak. In essence, vectorization allows R operators to take vectors as arguments for quick processing of multiple values. This is unlike some other programming languages such as C, C++, and Java, in which the processing of multiple values is usually done by iterating through and applying operators on each element of a vector (or array). R, being a flexible language, allows users to program using either iteration or vectorization. However, most of the time, iteration incurs significant and unnecessary computational cost because R is an interpreted, not compiled, language.

Take for example, the following simple code. Its goal is simply to calculate the square of every element in the random vector data. The first approach is to set up a for loop through every element of data and square it individually. Many would be tempted to take this approach because this is how it is done typically in other programming languages. Yet, a far more...