Introduction to R From Someone Who’s Never Coded Before

Pre-amble

This post is a short introduction to the coding language R from someone who has never coded before. R is a coding language used mostly for statistical computing and graphics, and as such is often used by statisticians for data analysis.

Although R is its own command line driven program, it is recommended to use a graphic user interface to help write, visualize and quality-check your own code. An example of an editor is RStudio, available on Windows, MacOS and Linux.

The Basics

Operators

Operators are a standard of mathematics, and as such are a key aspect of coding as a whole, R included. There are two types of operators:

Arithmetic Operators:
'+' (Addition, i.e. 1 + 2 returns 3)
'-' (Subtraction, i.e. 2 - 1 returns 1)
'*' (Multiplication, i.e. 2 * 2 returns 4)
'/' (Division, i.e. 6 / 3 returns 2)
'**' or '^' (Exponentiation, i.e. 2**3 returns 8)
'%%' (Modulus, i.e. 7%%3 returns 1)
'%/%' (Integer Division, i.e. 5%/% returns 2)

Logical Operators:
'>' greater than
'>=' greater than or equal to
'==' exactly equal to
'!=' not equal to

Functions

Like all programming, most work done in R is through the use of functions. In R specifically, a function is a defined section of code with the structure:

function (argument_list) {body}

The function is meant to carry out a task, but the functionality (pun intended) of said function is very much dependent on the needs and creativity of whoever creates it. It is possible that a function accepts arguments or parameters, but it isn’t necessary. It can also produce an output, but also is not a requirement for a successful function. R has several built in functions, which do not require a {body} as demonstrated in the following examples:

'median(x)' median
'rep(x, ntimes)' repeat x n times
'log10(x)' common logarithm

Packages

A neat thing about R (and some other languages too) is package installation. Packages are pre-made compliations of data, functions and general code in R with a specific format. These packages are stored in the ‘library’ directory and once downloaded can be used like the aforementioned built in functions. The command to install packages is as follows:

install.packages("Package_Name")

An example of a very useful R package is Tidyverse (it’s actually a collection of several packages all neatly tied into one), which contains the core packages of ggplot2, dplyr, tidyr, readr, purrr, tibble, stringr, and forcats. These can be used to clean-up code and then visualize, transform and even model the data analyzed.

To install Tidyverse, simply run

install.packages("Tidyverse")

as part of your code.