Statistics for Corpus Linguists
  • Overview
  • Introduction to R
    • 1.1 First steps
    • 1.2 Exploring R Studio
    • 1.3 Vectors
    • 1.4 Data frames
    • 1.5 Libraries
    • 1.6 Importing/Exporting
  • NLP
    • 2.1 Concordancing
    • 2.2 Regular expressions
    • 2.3 The CQP interface
    • 2.4 Data annotation
  • Statistics
    • 3.1 Data, variables, samples
    • 3.2 Probability theory
    • 3.3 Descriptive statistics
    • 3.4 Hypothesis testing
    • 3.5 Chi-squared test
    • 3.6 t-test
    • 3.7 ANOVA
    • 3.8 Case study
  • Models
    • 4.1 Linear regression
    • 4.2 Logistic regression
    • 4.3 Poisson regression
  • Machine Learning
    • 5.1 Tree-based methods
    • 5.2 Gradient boosting
  1. 1. Introduction to R
  2. 1.5 Libraries
  • 1. Introduction to R
    • 1.1 First steps
    • 1.2 Exploring R Studio
    • 1.3 Vectors
    • 1.4 Data frames
    • 1.5 Libraries
    • 1.6 Importing/Exporting

On this page

  • Recommended reading
  • Working with packages in R
    • Installation
    • Loading packages
    • Citing R and R packages

Other Formats

  • PDF
  1. 1. Introduction to R
  2. 1.5 Libraries

1.5 Libraries

Author
Affiliation

Vladimir Buskin

Catholic University of Eichstätt-Ingolstadt

Abstract
This handout explains how to install, load and cite R packages that are essential for data analysis and linguistic applications.

Recommended reading

Winter (2020): Chapter 1.13

Working with packages in R

Packages expand the basic functionality of R by providing numerous quality-of-life improvements that not only considerably simplify common data-wrangling tasks but also provide frameworks for state-of-the-art methods of statistical analysis and natural language processing (NLP), among many other things.

Installation

How do I install a package?

In RStudio, navigate to Packages > Install and verify that the pop-up window says Install from: Repository (CRAN). You can now enter the name of the package you would like to install in the Packages field.

Video tutorial on YouTube

Loading packages

Once the installation is complete, you can load the packages using the code below. You can ignore any warning messages that appear.

library(readxl)
library(writexl)
library(quanteda)
library(lattice)
library(tidyverse)
library(crosstable)
library(flextable)
library(confintr)
Activating packages

Whenever you start a new R session (i.e., open RStudio), your packages and their functions will be inactive. To reactivate a package, either use the library() function or simply tick its box in the Packages tab.

It is good practice to activate only those packages that are necessary for your analysis. While this is not a problem for the small set of packages shown here, loading dozens of packages increases the risk of ending up with ‘homonymous’ functions, i.e., functions that have the same name but perform different operations. In such cases, it can be helpful to ‘disambiguate’ them by explicitly indicating which package a function comes from:

readxl::read_xlsx(...)

Citing R and R packages

Whenever we draw on ideas other than our own, we give credit to the respective source by citing it appropriately. The same applies to R, RStudio and all the packages we rely on in our analyses.

For R, an up-to-date citation can be generated as follows:

citation()
To cite R in publications use:

  R Core Team (2024). _R: A Language and Environment for Statistical
  Computing_. R Foundation for Statistical Computing, Vienna, Austria.
  <https://www.R-project.org/>.

A BibTeX entry for LaTeX users is

  @Manual{,
    title = {R: A Language and Environment for Statistical Computing},
    author = {{R Core Team}},
    organization = {R Foundation for Statistical Computing},
    address = {Vienna, Austria},
    year = {2024},
    url = {https://www.R-project.org/},
  }

We have invested a lot of time and effort in creating R, please cite it
when using it for data analysis. See also 'citation("pkgname")' for
citing R packages.

To cite a specific package, simply supply its name as an argument to citation():

citation("quanteda")
To cite package 'quanteda' in publications use:

  Benoit K, Watanabe K, Wang H, Nulty P, Obeng A, Müller S, Matsuo A
  (2018). "quanteda: An R package for the quantitative analysis of
  textual data." _Journal of Open Source Software_, *3*(30), 774.
  doi:10.21105/joss.00774 <https://doi.org/10.21105/joss.00774>,
  <https://quanteda.io>.

A BibTeX entry for LaTeX users is

  @Article{,
    title = {quanteda: An R package for the quantitative analysis of textual data},
    journal = {Journal of Open Source Software},
    author = {Kenneth Benoit and Kohei Watanabe and Haiyan Wang and Paul Nulty and Adam Obeng and Stefan Müller and Akitaka Matsuo},
    doi = {10.21105/joss.00774},
    url = {https://quanteda.io},
    volume = {3},
    number = {30},
    pages = {774},
    year = {2018},
  }

References

Winter, Bodo. 2020. Statistics for Linguists: An Introduction Using r. New York; London: Routledge.
1.4 Data frames
1.6 Importing/Exporting