Skip to Main Content

Learn R

This guide focuses on transformation and cleaning functions in R that are especially useful for working with tabular datasets.

Introduction to R Environment

The R environment refers to your current workspace. You can think of it like the memory of all assigned values, which you can draw on as you work. For example, if 33 is assigned to black (“black<-33”), then “black” now exists within the R environment with a value of 33, and if the environment is cleared, then “black” will no longer have that value.

Packages in R

R is a bare bones program. Its utility comes from the 3500+ packages available for installation. You may think of these packages as similar to extensions or apps, which some other programs use, like Google, Chrome or Apple products. Each package serves a purpose and has specific commands you can use. In short, Packages are user-created programs which can be used to run a specific task or set of tasks.

Using packages is a twostep process. First you must install the package you want, and then you must load it into R.

Installing Packages

>install.package(“nameofpackage”) # installs a package
>library(“nameofpackage”) # loads an already installed package, you can use a package only after loading it
>installed.packages() # lists installed packages (not necessarily loaded)

One can install multiple packages at once with a single call by placing the names of the packages in a character vector.

install.packages(c(“tidyr”, “devtools”, “dplyr”, “ggplot2”))

Working Directory

R (and RStudio) employs the notion of a global working directory.

Any workspace images (.RData files) are stored here. Data can be imported into R directly from the working directory, without specifying the complete path of the file name.

There are 2 functions in R to get and set the working directory location.

getwd() # returns current working directory

setwd(“/path/to/new/working/directory”) # changes current working directory

Liaison Librarian

Profile Photo
Martin Morris
Contact:
Schulich Library of Physical Sciences, Life Sciences and Engineering
Macdonald-Stewart Library Building
809 rue Sherbrooke Ouest
Montréal, Québec H3A 0C1
(514) 398 8140
Website Skype Contact: martinatmcgill
Social: Twitter Page

McGill LibraryQuestions? Ask us!
Privacy notice