# Programming with R – Function Basics

October 20, 2010
By

(This article was first published on Software for Exploratory Data Analysis and Statistical Modelling, and kindly contributed to R-bloggers)

One of the benefits of using R for statistical analysis is the programming language which allows users to define their own functions, which is particularly useful for analysis that needs to be repeated. For example, a monthly output from a database may be provided in a pre-determined format and we might be interested in running the same initial analysis on the data.

The function keyword is used to define a function and there is an optional list of function arguments that can be specified. Unlike some programming languages R provides a certain degree of flexibility with setting defaults for particular arguments and the way that the arguments are matched can sometimes cause unexpected behaviour. As such it is sensible to explicitly match a value to a particular argument, e.g. data = mydata, so that the matching is done as expected.

Consider a simple example of a function that we could write to calculate the volume of a cylinder. The cylinder itself has a radius and height, which will be the two arguments to our function. The basic definition of our function is as follows:

cylinder.volume = function(height, radius) {   }

The volume of a cylinder is pi * raidus * height which we add to our function and save as an object that is returned at the end of the function calculations. The last line of code in a function, by default, is assumed to be the return value.

cylinder.volume = function(height, radius) { volume = pi * radius * height   volume }

This is a very simple example of function and if we call the function with a radius of 5 units and height of 10 units then the answer that is returned is:

> cylinder.volume(10, 5) [1] 157.0796

There are a number of things that we can do to the function to improve it. For example, how should the function react if the user does not specify a height and/or radius? Also what happens if a negative value is submitted to either argument?

To leave a comment for the author, please follow the link and comment on his blog: Software for Exploratory Data Analysis and Statistical Modelling.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,