Chapter 1 R, Rstudio, and packages

Please download the most recent version of R (R Core Team 2023). RStudio will make your life much easier, so please download that too. This document was made in RStudio via R Markdown (Allaire et al. 2023), knitr (Xie 2015), and bookdown (Xie 2023).

1.1 R

R is “a freely available language and environment for statistical computing and graphics which provides a wide variety of statistical and graphical techniques”. R is thus a programming language but also an environment in which you can code and do some fancy statistical and graphical things (similar to, for instance, SPSS, Excel, or Stata).

We’ve seen several major advantages of R, but the downside of R is that it is reasonably hard to learn. R is made for statical programming and reproducible “behaviour”, not for ease of use for beginners. Particularly in the past, getting your data into R, and calculating some group means or correlations would be a significant task compared to the few clicks in SPSS. Luckily, the times, they are changing, and many developments make R much more easy to use and friendly for beginners. There is no doubt in my mind that the costs of learning R relative to other programs are well worth it compared to what you can gain from it. Even more so in this day and age where there is a major push for every part of the scientific process to be open and reproducible to others. For me personally, it was seeing the amazing graphs, and needing particular analyses that were not available in other software packages that made me move entirely to R.

1.2 RStudio

Rather than working directly in the R-environment, we’ll be working in the RStudio environment. *RStudio is a free environment for working with R, that makes programming in R much, much easier. It takes away many frustrations that one might have when working in the R-environment. I myself haven’t worked in the R-environment since I discovered RStudio. An added bonus of RStudio(’s developers) is that they make many great, easy to use packages for R. In summary, download RStudio (do note that you ALSO need to have R downloaded and installed).

1.3 Installing packages

R works with “packages”. That means that for some functionality to work (like ggplot), you need to install those packages first. If you want to make use of them during your R-session, you will also need to tell R that you will be using them. You can copy all code in this document to your own R/Rstudio terminal. All the text after the hasthag “#” are comments, and will not be run by R as code.

install.packages("ggplot2") # Installs the ggplot-package

We also need to run the packages, to let R know that we will use them in this session.

library(ggplot2) # Tell R you will use the functionality

References

Allaire, JJ, Yihui Xie, Christophe Dervieux, Jonathan McPherson, Javier Luraschi, Kevin Ushey, Aron Atkins, et al. 2023. Rmarkdown: Dynamic Documents for r. https://github.com/rstudio/rmarkdown.
R Core Team. 2023. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Xie, Yihui. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. http://yihui.name/knitr/.
———. 2023. Bookdown: Authoring Books and Technical Documents with r Markdown. https://github.com/rstudio/bookdown.