Getting Started With R

Hello world!

Welcome to the introductory post of this blog. We’re starting with the basics and first up is: R! R is a statistical programming language that has grown in popularity and has a variety of applications, including data analysis and visualization.

Starting Up

To download R, go to: https://cran.r-project.org/mirrors.html and pick your mirror.

RStudio is the preferred IDE (Integrated Development Environment) for R. For those of you new to this, IDEs are additional software applications that typically make working with a programming language easier and more manageable.

Download RStudio from: https://www.rstudio.com/

Installing and Loading Packages

Base R is quite powerful, but it has its limitations. Luckily, it’s an open-source language with a thriving community that creates packages, basically add-ons that expand the functionality of R, that are freely available.

Most popular packages can be installed by opening RStudio (or R) and typing the following:

install.packages(“package_name”)

Hit enter to execute. CRAN is the default repository of packages that R will use to search for the package that you requested, but there are others that you will have to specify the repository for if they are not in CRAN. There is also always the option to download and install the package manually, but the install.package() function is extremely user friendly and will get you what you need most of the time.

Packages that are not part of base R need to be called before they are used every time you start up a new session. This can be done by either :

library(package_name)
or
require(package_name)

Both will do the job and note the lack of quotation marks around the package_name.

Keeping R Updated

R gets version updates fairly often and it’s a good idea to stay up to date (unless you want to stick with a certain version for a project). To update, open up R (not RStudio, RStudio will typically suggest that you do not use it to run the update), and type:

install.packages(“installr”) #if you do not have the package installed already
require(installr)
updateR() #will check if your version of R is the most recent and will update if it is not

This can also be done in one line:

install.packages(“installr”); require(installr); updateR()

There will be a few prompts, but it should be a straightforward process.

Don’t forget to open RStudio and click Help -> Check for Updates to update RStudio as well, if needed.

You can also occasionally check for package updates through RStudio by clicking Tools -> Check for Package Updates… (You will also notice that there is an option to install packages in the Tools menu, but it’s good to get in the habit of installing packages via install.packages).

Learning the Basics of R Programming

Last, but not least, here’s a number of websites to get you started on using R:

https://www.codecademy.com/
https://www.datacamp.com/

An alternative is a package called swirl, which can be installed and used directly in R to learn R (yo dawg, I heard you like R…).

Type:

install.packages(“swirl”)
require(swirl)

Follow a few prompts and you will see the following menu:

  1. R Programming: The basics of programming in R
  2. Regression Models: The basics of regression modeling in R
  3. Statistical Inference: The basics of statistical inference in R
  4. Exploratory Data Analysis: The basics of exploring data in R
  5. Don’t install anything for me. I’ll do it myself.

If you’d like to get started with R Programming, pick option 1, and you’ll be offered yet more options to explore what R can do with hands on tutorials.

If you ever get stuck, Google is your best friend when it comes to looking up bugs and errors, or how to do something!

Good Practices

Last, but not least, here’s some tips for good practices for coding in R (and beyond).

Above, you may have noticed the “#” symbol in the following line:

install.packages(“installr”) #if you do not have the package installed already

The “#” symbol denotes a comment in R and anything following that symbol will be ignored by it. The comment can go multiple lines and ends when you hit Enter. Comments are useful in order to keep code organized and clear, for yourself and others, so that it can be read and easily understood.

Google has published a style guide regarding naming conventions, spacing in code, and layout, found here: https://google.github.io/styleguide/Rguide.xml 

Again, the idea is to make the code more readable, easier on the eyes, and easier to follow. Not only is sharing code in the R community common, scientific journals are more and more requesting the code used for data analysis for review. So write your code like you expect someone else to read it, even if that someone is you years down the line when you’re writing your thesis, and you’ll thank yourself for it.

Questions? Comments? Leave us a message!

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *