Excel Unplugged

R Beginner Tutorial

In this beginner R tutorial, we’ll start by setting up RStudio and running some basic code samples. We’ll look at how RStudio looks and feels, how to run and save the code, how to install packages, and more.

Buckle in!

Why R?

R is a programming language and software environment for statistical computing and graphics. It is free and open-source. It first appeared in 1993 and has gone through a number of releases. Consequently, today R is widely used for data analysis among statisticians and data scientists.

Further, R is a language designed specifically for data analysis and plotting. This differentiates R from Python in the sense that you may find algorithms and functions in R that are not yet covered in Python.

History

In 1991 Ross Ihaka and Robert Gentleman of the University of Auckland, New Zealand, began an alternative implementation of the S language. They named it partly after their first names and partly as a play on the name of S. The first official release came in 1995. The Comprehensive R Archive Network (today famously known as CRAN) was announced in 1997 with 12 contributed packages.

Environment

R has a command-line interface, but there are multiple third-party graphical user interfaces available:

  • RStudio, an integrated development environment
  • Jupyter, an online notebook interface

Setting up RStudio

RStudio is a free environment for R scripting. You can download RStudio at https://www.rstudio.com/.

RStudio workspace consists of 4 panes:

  1. Source pane for viewing and writing R scripts
  2. Console/Terminal pane for live coding
  3. Environment/History/Connections pane for exploring active variables, viewing older commands and connections
  4. Files/Plots/Packages/Help pane for viewing plots and help and exploring files or packages
R introduction and workspace prepaparation - RStudio has four panes
Rstudio consists of four panes: Source, Console, Environment and Plots pane

Executing code

We can use Console for any quick coding. For example, let’s assign strings »Hello« and »world« to variables a and b, and concatenate them using paste() function.

R introduction and workspace prepaparation - Console
Executing live code in RStudio
a = "Hello"
b = "world"
paste(a, b)

We usually use the Source window for scripting when we want to save our work to a file.

R introduction and workspace prepaparation - Source
Executing code from script in RStudio

One way to run the code is by selecting Run the current selection button.

R introduction and workspace prepaparation - Running code
Executing code from script in RStudio

But the faster way is to use the CTRL + Enter shortcut.

Variable explorer

We can see all our objects in Environment pane.

R introduction and workspace prepaparation -Environment
Environment in RStudio

The pane lists a table of variable names and their corresponding values. For example, we see variable a is a string of value “Hello”.

Clearing variables

To clear a variable we use the rm() command. For instance, let’s clear variable a.

rm(a)

But what if we want to clear all the variables? We use the Clear button.

R introduction and workspace prepaparation - Clearing variables
Clearing variables in RStudio

Similarly, we can also use the following command.

rm(list = ls())

R files

We can save our code to file by selecting File > Save As.

R introduction and workspace prepaparation - Save As
Saving R scripts in RStudio

After that we select the location and confirm with OK. Consequently, R file is created. All R scripts end with .R.

Packages

There are number of available R packages, helping you code your ideas faster. You can check the list at Comprehensive R Archive Network, or CRAN for short at https://cran.r-project.org/.

Here are some of the most famous ones:

  1. ggplot2, famous library for beautiful visualizations
  2. dplyr, data manipulation library
  3. forecast, library for forecasting time series
  4. lubridate, library for manipulating date-time objects
  5. shiny, library for building interactive web apps

Installing and using packages

To install a package we use install.packages() function. For example, to install ggplot2 we use the following command.

install.packages("ggplot2") 

To import the library we use the library() function. For example, to import the ggplot2 library we use the following command.

library(ggplot2)

Base Functions

To list all R base functions we use the following command.

library(help = "base")
R introduction and workspace prepaparation - Help
Listing all R base functions in Rstudio

In conclusion, this sums up all the basics you need to know to navigate the stable RStudio waters. Continue to the next chapter and learn about the basic R syntax!

In a hurry?

If you’re already running late we recommend you head over to our two R crash courses:

For even more amazing tips check out our awesome ExcelOlympics YouTube channel!

R tutorial

R tutorial

Share this:

Share on email
Email
Share on linkedin
LinkedIn
Share on facebook
Facebook
Share on twitter
Twitter
Share on pinterest
Pinterest

Related posts