In this post, I cover an important aspect of using R that users of SPSS/Excel won’t be familiar with: working with packages. Packages and the package system form a major difference between R and SPSS/Excel, which is why I’m devoting this entire post to them. It’s the second post in a series aimed at people wanting to migrate from SPSS/Excel to using R full-time. The previous post on this topic is available here. Again, this post is aimed primarily at psychology researchers, as that’s what I am, though it will hopefully be relevant to others as well.
Packages in R
With SPSS/Excel, you pretty much get everything you could ever want to use, and more, installed with the default installation. This leads to a simple question. How many of the many hundreds of buttons, boxes and options in these programs have you used in total?
R is different. The basic installation of R comes with a large number of packages and commands. However, with R, people have been able to share their own packages which can help out, extend, and implement other useful things to make R even more funky and powerful. This is beneficial for a number of reasons, but, for the new user, it might seem a bit strange. Why doesn’t R just come with all the packages installed right away? Well, the chances are you won’t need all of the packages in existence, so there’s little point in installing them all by default. Doing so also reduces the size of an R download, saves hard drive space, and so on.
People are adding new and useful packages all the time, so let’s install a couple of popular ones that I use all the time.
Installing Packages in R
To get to the list of packages you have installed, go to the packages tab using RStudio:
Packages are often updated, so you can use the Check for Updates button to update your packages.
To install a new package, you can either run the following command via the script tab or console window:
Where PACKAGENAME is the name of the package. Alternatively, using RStudio, you can hit the Install Packages button in the Packages window. You’ll be greeted with something like the following:
In this window, just type the name of the package you want to install in the Packages text box. Here, I’ve gone for ggplot2 and plyr.
Once you hit the install button, the packages will be installed. It’s best to leave Install Dependencies checked because some packages need others to function. For example, ggplot2 uses plyr.
Loading Packages in R
The packages you have installed won’t be loaded straight away. If R loaded all the packages you had installed, then you would often end up with packages loaded that you don’t need to use. To load your packages, you can do one of two things. First you can run the command:
Where PACKAGENAME is the name of the package that you want to load.
An alternative method is to select the package using RStudio’s Package window. To load the package(s) that you want, all you need to do is click the checkbox next to the package name. See below.
There we go, ggplot2 has now been loaded! It’s also loaded plyr as ggplot2 needs plyr to function, as well as reshape.
Getting Help with Using a Package
Packages come with helpful documentation to get you started with using them. Again, you have two options in terms of accessing the documentation. First, you can type the command:
Where PACKAGENAME is the name of your package.
Alternatively, you can click the name of the package in RStudio’s Packages window, as below.
Whichever method you use, you’ll be presented with the documentation in your packages window, which you can browse to work out what you need to do to use the package.
Which Packages should you Install?
One of the daunting aspects of getting started with R is choosing how to use it, and what packages to install. I’ll cover some suggested packages in future guides, but for the eager, there’s a great list of popular packages that has been put up online by Matthew Dowle, and is available at this link. The list is also part of his unknownR package, which is worth trying out if you are new. When learning R, I used that list to inspire me in terms of which packages I should learn.
You should also keep an eye on community sites such as R-Bloggers, as you’ll often read about packages, as well as other tips and tricks, that you can use and learn from.
UPDATE: Thanks to Tal Galili’s comment, readers may also want to check out CRAN task views, which has detailed info on a huge range of packages.
In the next guide, I’ll get into the interesting stuff: importing and manipulating data, and how doing so differs from SPSS/Excel.