No history yet

Introduction to R

What Is R?

R is a programming language specifically designed for statistical analysis and data visualization. Think of it as a specialized toolbox built by statisticians, for anyone working with data. It was created in the early 1990s by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand. Their goal was to make a powerful, flexible, and accessible tool for data work.

One of R's biggest strengths is that it's open-source. This means it's completely free to use, share, and modify. A massive global community of developers and data scientists contributes to it, creating thousands of add-on packages that extend R's capabilities. Whether you're analyzing scientific research, tracking business metrics, or creating complex charts, there's likely an R package to help you do it efficiently. This community support has made R a go-to language in academia and the data science industry.

R is a language built by statisticians, for statisticians.

Your Toolkit R and RStudio

Before we start, it’s important to understand two key tools you'll be using: R and RStudio. They work together, but they are separate things.

  • R is the programming language itself. It's the engine that understands and executes your commands.
  • RStudio is an Integrated Development Environment (IDE). It’s a workbench that makes using R much easier. It gives you a code editor, a console, tools for viewing plots and data, and much more, all in one window.

You can use R without RStudio, but RStudio makes the whole process smoother and more organized. Think of R as a powerful engine and RStudio as the car built around it, complete with a dashboard, steering wheel, and comfortable seats.

You must install R first, then install RStudio. RStudio needs R to function.

First, let's install R. Go to the Comprehensive R Archive Network (CRAN) website. You'll see links to download R for Linux, macOS, and Windows. Click the appropriate link for your operating system and follow the installation instructions. Accept the default settings during installation unless you have a specific reason to change them.

Once R is installed, it's time to get RStudio. Visit the Posit website (the company that makes RStudio) and navigate to the RStudio Desktop download page. Choose the free version. Download the installer for your operating system and run it. Again, the default settings should work perfectly fine.

Exploring the RStudio Environment

When you first open RStudio, you'll see an interface divided into several panes. By default, there are three, but it's most common to work with four. Let's get familiar with the standard four-pane layout, which you can set up by going to File > New File > R Script.

Here's what each pane does:

  1. Source Editor (Top Left): This is a text editor where you'll write and save your R scripts. A script is just a text file with a .R extension that contains all your code. This allows you to save your work, share it, and run it again later.

  2. Console (Bottom Left): This is where R actually executes your commands. You can type code directly into the console to run it immediately, or you can send code from the Source Editor to the console. The results of your code will also appear here.

  3. Environment & History (Top Right): The Environment tab shows all the objects, like variables and datasets, that you've created in your current R session. The History tab keeps a record of all the commands you've run.

  4. Files, Plots, Packages & Help (Bottom Right): This is a multi-purpose pane. The Files tab lets you navigate your computer's file system. The Plots tab is where your graphs and charts will appear. The Packages tab shows you all the R packages you have installed, and the Help tab is for looking up documentation on R functions.

Now that you have R and RStudio installed and have a feel for the environment, you're ready to start working with data. Let's try a simple command. In the Console pane, next to the > prompt, type 1 + 1 and press Enter.

1 + 1

R will respond with:

[1] 2

Congratulations, you've just executed your first line of R code. The [1] simply indicates that the answer is the first element of the output. You're now set up and ready to dive deeper into R.