2 Functions in R

Peter McQuire

2.1 Introduction

2.1.1 Objectives

The objective of this chapter is to introduce the reader to using functions in R. We will, briefly, look at using some of the most important basic R functions, then proceed to write our own functions. It is expected that this chapter, together with Chapter 1, will be one of the first read by most users of this book. It provides a simple introduction to using R for the first-time user and provides material which is likely to be fundamental in future R programming.

There are generally three types of functions we encounter in R:

  • Core functions,
  • Package functions, and
  • User Defined functions

This chapter is split into two main sections: the first introduces pre-defined functions (Core functions and Package functions), and the second part introduces user-defined functions. We complete the chapter with a section which covers two commonly used functions: for and integrate - used to write loops and perform integration in R.

Please note that it is assumed throughout this chapter that the reader is new to programming. There may also be a small amount of repetition of some material from Chapter 1.

2.1.2 Core and Package Functions

Core functions in R are immediately available to use when the R programming language is first downloaded onto your computer. In addition there is a huge number of functions written by other R users which exist in packages. To use these functions we must first install and load the relevant package ...

Get R Programming for Actuarial Science now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.