O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

By operating on entire vectors at a time, vectorized operations eliminate the need for explicit loops. R processes loops inefficiently, because it interprets the statements in a loop over and over again. Thus, loops with much iteration tend to perform poorly. Vectorized operations help us to get around this bottleneck, while at the same time making our code compact and more elegant.

Several built-in functions are vectorized and step 1 illustrates this with the paste function that concatenates strings.

The later part of step 1 shows that, if the vectors have unequal lengths, then the shorter vector recycles the list of vectors as needed. The new.last.name vector of size 1 repeats itself to match the size of the first.name

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required