R really means functions

by Karl-Kuno Kunze

You probably have heard that R is a functional language. This post just reminds you of how far the concept goes.

Example 1

Simple and clear: Functions go like this:

More than one statement are written inside curly brackets.

However, the curly brackets represent themselves a function. Therefore, this:

works as nicely.

Example 2

Simple and clear: The selection operator for vectors and the like.

How about this:

In this way you can use selections in apply statements e.g. for rolling averages

The anonymous function calculates averages of subseries of mySeries starting at n with a window size of winLength. The only argument is n. This function is applied to the series starting at 1 and ending at 7.