R Packages

The post is about R Packages in the form of Questions and Answers.

Introduction to R Packages

Question: What is an R Package?

Answer: An R package is a standardized way to bundle:

  • Functions (Reusable code for tasks like data cleaning or modeling).
  • Datasets (Sample data for practice, e.g., mtcars).
  • Documentation (Help files, vignettes, and examples).

Question: List some popular R Package sources.

Answer: Popular R package sources are

  • CRAN (Comprehensive R Archive Network) – Official repository (10,000+ packages).
  • GitHub – Cutting-edge or development versions.
  • Bioconductor – Bioinformatics-focused packages.

Installing R Packages

Question: How to install an R Package?

Answers: The following are some examples of installing an R package on a computer:

From CRAN (Recommended for Beginners)

install.packages("dplyr")                # Install a single package
install.packages(c("ggplot2", "tidyr"))  # Install multiple

From GitHub (Development Versions)

install.packages("devtools")        # Needed first  
devtools::install_github("tidyverse/dplyr")  

Checking Package Version and Installed R Packages

Question: What version of R do I run on my computer or laptop?

Answer: To get the information about the version of R, use the following command at the R prompt.

# get a version of R
R.version.string

You will get a result like

[1] “R version 3.2.1 (2015-06-18)”

Note that a package in R language is a collection of objects that R Language can use. A package contains functions, data sets, and documentation (which helps how to use the package) or other objects such as dynamically loaded libraries of already compiled code.

Question: How to check what packages are already installed?

Answer: To get a list of installed packages, write “library()” without quotation marks at the prompt. You will see the list of all of the packages installed in the local R directory of your computer system, and then it will list all packages installed globally on your computer system.

# list all packages installed
library( )

You would get results like (note that results below are given as an example only, it’s not a complete list)

in library ‘C:/Users/abcd/Documents/R/win-library/3.2’:
combinat     Combinatorics utilities
proftools      Output Processing Tools for R
rgl                3D visualization device system (OpenGL)

Packages in library ‘C:/Program Files/R/R-3.2.1/library’:
KernSmooth      Functions for kernel smoothing for Wand & Jones (1995)
MASS                Support Functions and Datasets for Venables and Ripley’s MASS
Matrix               Sparse and Dense Matrix Classes and Methods
methods           Formal Methods and Classes
mgcv                Mixed GAM Computation Vehicle with Automatic Smoothness Estimation

Following is a very short list of packages installed in the local library.

Packages in library ‘C:/Users/imdad/Documents/R/win-library/3.5’:

abind               Combine Multidimensional Arrays
AlgDesign      Algorithmic Experimental Design
askpass          Safe Password Entry for R, Git, and SSH
assertthat      Easy Pre and Post Assertions
tibble               Simple Data Frames
plyr                  Tools for Splitting, Applying and Combining Data

Available R Packages in Local and Global Directory

Essential R Packages for Beginners

Question: List some popular/ essential R packages that are useful for data analysis and visualizations.

Answer: The following is the list of essential R packages for beginners:

PackagePurposeExample Use Case
dplyrData manipulationFilter, sort, and summarize data
ggplot2Data visualizationCreate charts and graphs
tidyrData cleaningParse and manipulating dates
readrFast data importRead CSV/Excel files efficiently
lubridateDate-time handlingParse and manipulate dates

Managing R Packages

Question: How can one update and remove a package?
Answer: The following commands can be used to update and remove an R package from a system.

update.packages()         # updates all installed packages
remove.packages("dplyr")  # uninstall a package

Summary

R packages are essentially a combination of reusable code, documentation, and code that extend the power and capabilities of the R programming language. They are designed to be easily installed and used. The R packages are a major reason why R is so popular in data science. There are tens of thousands of R packages available on CRAN and other repositories, covering a wide range of tasks, from data manipulation and analysis to visualization and modeling.

For further details on R Packages, see the link Packages in R Language.

Learn Basic Statistics