Packages & Libraries | Dataplexa

Packages & Libraries in R

In this lesson, you will learn how to work with packages and libraries in R. Packages allow you to extend R’s capabilities by adding ready-made functions, datasets, and tools created by the R community.

Instead of writing everything from scratch, you can install packages and use powerful features with just a few lines of code.


What Is a Package in R?

A package is a collection of functions, documentation, and sometimes datasets that solve specific problems.

For example, some packages help with data manipulation, others with visualization, statistics, or machine learning.


What Is a Library?

A library is the place on your computer where installed packages are stored.

When you load a package using library(), R makes its functions available for use in your current session.


Checking Installed Packages

You can see all packages currently installed on your system using the following command.

installed.packages()

This displays package names, versions, and other useful details.


Installing a Package

If a package is not installed, you must install it before using it.

Installation is done only once per system.

install.packages("dplyr")

R downloads the package from an online repository and stores it in your library.


Loading a Package

After installation, you must load the package every time you start a new R session.

library(dplyr)

Once loaded, all functions from the package become available.


Using Functions from a Package

After loading a package, you can directly use its functions.

Below is an example using a function to select columns from data.

data <- data.frame(
  name = c("Alex", "John", "Emma"),
  age = c(25, 30, 28)
)

select(data, name)

This makes data manipulation faster and easier.


Using a Package Without Loading It

Sometimes you may want to use only one function from a package.

You can do this using the :: operator.

dplyr::select(data, age)

This avoids loading the entire package into memory.


Popular Categories of R Packages

R packages are grouped by their purpose.

  • Data manipulation packages
  • Data visualization packages
  • Statistical analysis packages
  • Machine learning packages

As you progress, you will work with many specialized packages.


Updating Packages

Keeping packages updated ensures you get bug fixes and new features.

update.packages()

R will prompt you before updating each package.


Removing a Package

If a package is no longer needed, you can remove it from your system.

remove.packages("dplyr")

📝 Practice Exercises


Exercise 1

Install a package of your choice and load it into R.

Exercise 2

List all installed packages on your system.

Exercise 3

Use a package function without loading the package using ::.

Exercise 4

Update all installed packages.


✅ Practice Answers


Answer 1

install.packages("ggplot2")
library(ggplot2)

Answer 2

installed.packages()

Answer 3

stats::mean(c(10, 20, 30))

Answer 4

update.packages()

What’s Next?

In the next lesson, you will learn how to import data from files such as CSV and Excel into R.

This is an essential skill for real-world data analysis.