First Steps in R
Download the PDF R overview to get an overview about R and a list of helpful resources (you need to download the file in order to use the embedded links).
The first step is to install R. You can download and install R from the Comprehensive R Archive Network (CRAN).
Windows: - Open the Comprehensive R Archive Network. - Click on “CRAN”. You’ll see a list of mirror sites, organized by country. - Select a site near you. - Click on “Windows” under “Download and Install R”. - Click on “base”. - Click on the link for downloading the latest version of R (an .exe file). - When the download completes, double-click on the .exe file and answer the usual questions.
Mac: - Open the Comprehensive R Archive Network. - Click on “CRAN”. - You’ll see a list of mirror sites, organized by country. - Select a site near you. - Click on “MacOS X”. - Click on the .pkg file for the latest version of R, under “Files:”, to download it. - When the download completes, double-click on the .pkg file and answer the usual questions.
The next step is to install RStudio, a free and open-source integrated development environment (IDE) for R. You can use it for viewing and running R scripts.
- Go to RStudio Download
- Click the Download RStudio Desktop button.
- Select the installation file for your system.
- Run the installation file.
Learn R Basics
First of all, you can take an online course to master the basics of R: Visit the interactive R-Course from DataCamp. With the knowledge gained in this courses, you will be ready to undertake your first very own data analysis.
There are also open and free resources and reference guides for R. Two examples are:
- Quick-R: a quick online reference for data input, basic statistics and plots
- R reference card (PDF) by Tom Short
Two key things you need to know about R is that you can get help for a function using
?, like this:
and the hash character represents comments, so text following these characters is not interpreted:
##This is just a comment
Installing R Packages
The first R command we will run is
An R package is a collection of functions, data, and documentation that extends the capabilities of base R. Many of these functions are stored in CRAN. You can easily install packages from within RStudio if you know the name of the packages.
As an example, we are going to install the
dplyr which we use in our first data
We can then load the package into our R sessions using the
From now on you will see that we sometimes load packages without
installing them. This is because you only need to install a package once,
but you need to reload it with the command
library every time you start
a new R session.
If you try to load a package and get an error, it probably means you need to install it first.
Review the dplyr-documentation to get an overview about the different functionalities of this package.