COSMOS logo

R Packages

Zhaoxia Yu

R Packages

R comes with basic functionality. Packages extend R by adding new functions, datasets, and documentation. Think of packages as “apps for R.”

Example.

Without packages With packages
Basic plots Beautiful plots with ggplot2
Basic regression Survival analysis with survival
Read CSV Read Excel with readxl

Importance

  • New statistical methods are often released as R packages before they appear in commercial software.

  • Many research papers provide an accompanying R package so others can reproduce the analysis.

  • Today, CRAN hosts over 24,000 active packages.

  • Packages are one of the biggest reasons R has become so popular among statisticians and data scientists.

Open Source: Procs

  • Free
  • Thousands of packages
  • Rapid development
  • Transparent source code
  • Community support
  • Reproducible research

Open Source: Cons

  • No official support
  • Quality varies
  • Documentation can be inconsistent
  • Some packages are no longer maintained
  • Different packages may do similar things
  • Updates can occasionally break older code

Some Famous R Packages

Package Purpose
ggplot2 Graphics
dplyr Data manipulation
tidyr Data cleaning
readr Import data
survival Survival analysis
lme4 Mixed models
glmnet LASSO
shiny Web apps

Types of R Packages - by Purpose

Type Examples
Data packages palmerpenguins, nycflights13
Data wrangling dplyr, tidyr
Visualization ggplot2
Statistical modeling survival, lme4, glmnet
Machine learning caret, tidymodels, randomForest
Reporting knitr, rmarkdown, Quarto
Web applications shiny

Types of R Packages

  • Single packages are designed for a specific purpose and contain a set of functions, datasets, and documentation.

    • ggplot2 – data visualization
    • dplyr – data manipulation
    • survival – survival analysis
  • A meta-package is a package that contains other packages. A set of related packages can be downloaded at once.

    • tidyverse - a collection of packages for data science, including ggplot2, dplyr, tidyr, and more.

Is a package good?

A practical checklist:

  • Is it on CRAN?
  • Is it actively maintained?
  • Is it widely used?
  • Is there good documentation?
  • Is it cited in papers or textbooks?
  • Does it have recent updates?

Install and Use a Package

install.packages("ggplot2")   # once

library(ggplot2)              # every new R session

Example: Ecdat

  • The package Ecdat contains data sets for econometrics. It has a dataset called CRANpackages that contains the number of R packages on CRAN over time.
Code
if(!require('Ecdat')) {
  install.packages('Ecdat')
  library('Ecdat')
}
plot(Packages ~ Date, CRANpackages, log = "y", ylab = "n (log scale)", xlab = "Year", main="near-exponential growth in its early years")

Example: tidyverse

#please download and install the tidyverse package first if you haven't done so.
library(tidyverse) 

Example: tidyverse

  • Base R version
alzheimer_data <- read.csv('data/alzheimer_data.csv') 
alzheimer_data <- alzheimer_data[, c("id", "diagnosis", "age", "educ", "female", "height", "weight")]
alzheimer_data$diagnosis <- as.factor(alzheimer_data$diagnosis)
alzheimer_data$female <- as.factor(alzheimer_data$female)
  • Tidyverse version
alzheimer_data <- read.csv('data/alzheimer_data.csv') %>% 
  select(id, diagnosis, age, educ, female, height, weight) %>% 
  mutate(diagnosis = as.factor(diagnosis), female = as.factor(female))