Overview of Object-Oriented Programming in R by Joseph Adler

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

O'Reilly logo

Overview of Object-Oriented Programming in R

Object-oriented programming is not the same thing as programming with objects. R is a very object-centric language; everything in R is an object. However, there is more to OOP than just objects. Here’s a short description of what object-oriented programming means.

Key Ideas

As an example of how object-oriented programming is used in R, we’ll consider time series.[28] A time series is a sequence of measurements of a quantity over time. Measurements are taken at equally spaced intervals. Time series have some properties associated with them: a start time, an end time, a number of measurements, a frequency, and so forth.

In OOP, we would create a “time series” class to capture information about time series. A class is a formal definition for an object. Each individual time series object is called an instance of the class. A function that operates on a specific class of objects is called a method.

As a user of time series, you probably don’t care too much about how time series are implemented. All you care about is that you know how to create a time series object and manipulate the object through methods. The time series could be stored as a data frame, a vector, or even a long text field. The process of separating the interface from the implementation is called encapsulation.

Suppose that we wanted to track the weight history of people over time. For this application, we’d like to keep all the same information as a time series, plus some additional ...

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