Setup on the Local Machine

Learn how to set up R on your local machine and get some tips for using RStudio.

Obtaining R

If you are brand new to this, you will have to download R in order to do anything. Just navigate your web browser to http://cran.rproject.org to download the appropriate R version for your operating system. There is another program you may have heard of and may want to use called RStudio, which can be found at http://www.rstudio.com.

RStudio

Please remember that RStudio is a program that uses R. It helps keep things organized and has some nice autocomplete functions, but R is the actual program that does everything that we have covered in this course. RStudio has plenty of great features, don’t get me wrong. It’s really great for writing in RMarkdown and LaTeX if you choose to do that. R does all the heavy lifting. R is the statistics program.

Installing and loading packages

R is designed to be a small program (currently just about 80 MB) which makes it easy to download and install anywhere in the world. The base version of R contains a great number of functions for organizing and analyzing data, but the real strength comes in what are called packages. Packages are freely downloadable additions to R that provide new functions and datasets for particular analyses. For example, the base version of R can conduct linear models and generalized linear models but cannot conduct mixed effects models. To do mixed effects models, you need to download a specific package (of which there are several).

The only important thing to remember about packages is that adding them to R is a two-step process. First, you have to install a package, which (perhaps counterintuitively) just downloads the package to your computer. Secondly, you have to load the package, which is when you have actively placed it in the current memory for use. You will generally obtain packages from the Comprehensive R Archive Network (CRAN) directly through R.

Install some packages

Assuming you have installed R on your computer, you should run the following code to install the various packages you will need to have in order to execute the commands presented throughout this course.

If you are using RStudio, you can click on the packages tab and search for this one at a time by using the little search window. Make sure to click the button to “Install Dependencies.”

install.packages(c(”lme4”,”multcomp”,”car”,”ggplot2”,”gplots”,
”MASS”,”tidyr”,”dplyr”,”broom”,”gridExtra”,
”cowplot”,”emmeans”,”glmmTMB”,”lattice”),
dependencies = T, repos =
”http://cran.us.r-project.org”)

File paths on a Mac vs. PC

It isn’t always easy to figure out exactly where on your computer a file is located. Luckily, if you use a Mac, there is a trick built into R to make it easy. Just drag the icon of the file from a Finder window into your script window, and your computer will paste in the exact address of the file for you. Note that this does not work in RStudio, just in regular old R. On a PC, it is not as simple. You can copy the address from the file and paste it into your script window, but know that for whatever reason, it pastes in the address with the forward slashes (/) in the wrong direction as backslashes (\), and you will have to change that manually.

Using RStudio

Under the File menu, there is an option titled “Import Dataset,” and you can choose to import the data from your .csv file. In the current version of RStudio, there are two options for how to import the data. The first uses base R, which means it uses read.csv(). The nice thing here is that you can navigate to where the file is on your computer, just like any other file. You can also select a box to tell it to treat your categorical data as factors. The second option uses the package readr, which utilizes the function read_csv(), which does not read in the file as a data frame, but instead as a format called a tbl_df, which is slightly different. This is a newer format preferred by RStudio, but the annoying thing is that the format does not always play nice with some of R’s older built-in functions. Additionally, there is no way to tell read_csv() to automatically make your character data factors, so you would have to do it manually. Evidently, read_csv() is much faster at reading in enormous files.

Saving your hard work out of R

You might think it is a good idea to save your work out of R to your hard drive or the cloud, thereby allowing you to view it in another program, email it to a collaborator, or import it at a future point in time without re-running all your code again. Just like when you used the function read.csv() to read your data, you can use its parallel function write.csv() to get your data back out of R and save as a file. All you have to provide is one argument: the R object you want to save. You can (and probably should) also specify a path (in quotes) for where you want the file to be saved, but if you don’t, R will save it to your working directory. I would also recommend a third argument, which specifies that you don’t want R to create a new column that lists the row numbers of your data frame (indicated with row.names=FALSE). For example, if you are working on a Mac and want to save RxP.byTank as a file on your desktop, you would type the following:

#This will create a .csv file on my desktop called RxP_byTank.csv!
write.csv(RxP.byTank, ”~/Desktop/RxP_byTank.csv”,
         row.names=FALSE)

It is a good idea to have a general naming convention for your variables, data objects, etc. We would suggest using the underscore (_) to create spaces in actual file names on my computer and generally use a period (.) to create spaces within R. You might have a different preference, which is perfectly fine.

Note that when you read the file back in, any factors will be back in the default alphabetical order (i.e., Pred will be ordered C, L, NL instead of C, NL, L).

Get hands-on with 1200+ tech skills courses.