1 The Paradigms of R

There are three main programming paradigms–or styles–that R uses: Array Programming (AP), Functional Programming, and Object Oriented (OOP). Knowing how these paradigms work is important, as they will help one understand the syntax structure of R.

1.1 Array

The Array Programming (AP) paradigm allows us to access elements in a dataset via a matrix-like syntax. This paradigm can be useful when you want a more granular, so to speak, way of subsetting data, as you are able to specify the numeric positions of the desired rows and columns.

# Select the 2nd row and 5th column
#   from mtcars, which is a pre-loaded dataset.
mtcars[2, 5]
## [1] 3.9
# Select the first 5 rows and all columns
mtcars[1:5, ]
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

1.2 Functional

Much like Excel, R has functions: execution statements with an input and an output–this syntax style is called Functional Programming (FP). The benefit of FP is that we can think more at a “high level”: we simply input some data, and the function processes it without our stating of how we want to execute the information. In other words, we can treat functions as “black boxes” that output our desired result based on our specified inputs.

Below is an example of using the mean() function to calculate the average miles per gallon from the mtcars dataset.

# Mean of MPG from the mtcars dataset
# Input  = mtcars$mpg
# OUtput = numeric value

mean(mtcars$mpg) # $ accesses MPG from mtcars.
## [1] 20.09062

Just like in math and Excel, we can compose multiple functions together.

# Rounding the mean MPG by 2 digits.
round(mean(mtcars$mpg), 2)
## [1] 20.09

1.3 Object Oriented

In R, we can create and access objects, which are a storage of information with attributes: this paradigm is called Object Oriented Programming (OOP), which is concerned about classes and types–the “foreground” and “background” characteristics of a dataset, so to speak. Classes affect how data look to the user, whereas types are the specific attributes of some data.

Classes and types are discussed in the Basics chapter.

# Access the MPG variable from mtcars
#   and save it to an object named "x"
x <- mtcars$mpg

# We can now refer to mtcars$mpg anytime with "x"
x 
##  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4

We can check the structure of our data objects to know their attributes.

# Check the structure of mtcars.
# A data frame composed of numeric vectors.
str(mtcars) 
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

1.4 Summary

Table 1.1: Summary of Paradigms
Paradigm Description Example
Array Bracket syntax structure to access data like a matrix mtcars[2, 5]
Functional Mathematical function syntax structure to compute over data. mean(mtcars$mpg)
Object Oriented Syntax structure in which data has stored attributes that affect how they look to the user. str(mtcars)