Introduction
R is an open-source programming language and environment for statistical computing and data visualization. R's extensive collection of libraries and packages supports various statistical techniques and models such as hypothesis testing, regression analysis, time series analysis, and more.
Data scientists and statisticians use R to manipulate, calculate, and graphically display data in the form of statistical plots and graphs.
Learn how to install R on Ubuntu 20.04 or Ubuntu 22.04 and start working with datasets in no time.
Prerequisites
- Ubuntu 20.04 or Ubuntu 22.04 installed.
- A user account with root or sudo privileges.
- Access to a command line/terminal window (Ctrl+Alt+T).
How to Install R on Ubuntu
There are two ways to install R on Ubuntu:
- Install R from Ubuntu repositories. The installation process is simple and requires only a few steps. However, since the default Ubuntu libraries are not updated as frequently as the CRAN repositories, there is a possibility of installing an older version of R.
- Install R from the CRAN repository. CRAN (Comprehensive R Archive Network) is backed by the R Foundation and serves as the primary software repository for R. It hosts a collection of current and past versions of the R distribution, along with documentation and contributed R packages. While the installation process from CRAN may involve additional steps compared to the Ubuntu repositories method, it always includes the latest available R version.
Note: The provided commands can be used for both Ubuntu 20.04 (Focal Fossa) and 22.04 (Jammy Jellyfish).
Method 1: Install R from Ubuntu Repository
To install R from the Ubuntu repository:
1. Open the terminal and update the apt package lists:
sudo apt update
2. Install the R base package and its dependencies using the following command:
sudo apt install r-base r-base-dev -y
The r-base-dev
package enables users to install new packages using the install.packages()
function within the R console.
3. Type R
and hit enter to verify that R has been installed:
R
Note: Using the R
command without sudo creates a personal library for your user. To install packages available to every user on the system, run the R
command as root by typing sudo -i R
.
R has been successfully installed, and you have access to the R console prompt.
Type q()
to exit the R console.
Method 2: Install R on Ubuntu from CRAN Repository
To install R from the CRAN repository:
1. Update the apt package lists:
sudo apt update
2. Install the software-properties-common
and dirmngr
helper packages for managing CRAN repositories and certificates:
sudo apt install software-properties-common dirmngr -y
3. Download and add the signing key for the CRAN repository:
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
4. The CRAN repository key fingerprint is E298A3A825C0D65DFD57CBB651716619E084DAB9
. Verify the downloaded key using the following command:
gpg --show-keys /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
5. Add the CRAN repository to your system:
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
When prompted, press Enter
to start the download.
Using $(lsb_release -cs)
instructs the system to automatically recognize which Ubuntu version your machine is running.
Note: Alternatively, you can replace $(lsb_release -cs)
with your Ubuntu version, for example, jammy
for Ubuntu 22.04 or focal
for Ubuntu 20.04.
6. Enter the following command to install the base R package and its dependencies from the CRAN repository:
sudo apt install r-base r-base-dev -y
The r-base-dev
package allows users to install packages from the R console using the install.packages()
function.
7. Enter the R
command to access the R console:
R
Note: Using the R
command without sudo creates a personal library for your user. To install packages that are available to every user on the system, run the R
command as a root user by typing sudo -i R
.
You have successfully installed the latest R version (4.3.0) from the CRAN repository.
Installing R Packages
The R community actively develops and maintains thousands of packages for specific tasks like data visualization, machine learning, statistical modeling, text mining, etc. Some of the most popular R packages include:
- dplyr. This package provides practical and intuitive functions for summarizing, filtering, and transforming data.
- tidyr. The tidyr package enables users to reshape and organize messy data into a tidy format.
- purrr. Statisticians use the purrr functional programming package to improve flexibility and maintain consistency when working with functions and vectors.
- ggplot2. ggplot2 is a versatile R package for creating and customizing visually appealing plots.
- data.table. The high-performance data.table package is primarily used for data manipulation in large datasets.
- stringr. The stringr package provides functions for streamlining string and text manipulation tasks.
Users can install R packages from within the R console by utilizing the install.packages("package_name")
function.
For example, to install the ggplot2 package from the CRAN repository, access the R console and enter the following command:
install.packages("ggplot2")
Note: To install a different package, replace ggplot2 with the name of the desired package.
If you encounter the 'lib = "/usr/local/lib/R/site-library"' is not writable
prompt, type yes
to use and create a personal library for your user.
Once the installation process is complete, load the ggplot2 library:
library(ggplot2)
The ggplot2 package comes with preinstalled datasets to allow users to test the features. Use the data()
function to view a list of preinstalled datasets:
data()
Press q
to exit the dataset list and return to the R console.
In this example, the summary()
function is called to review the variables in the Orange dataset:
summary(Orange)
Use the following command to plot the numeric and categorical variables in the Orange dataset:
ggplot(Orange, aes(x=Tree, y=circumference, col=age))+geom_point()
R generates a visual statistical plot in a new window.
The ggplot2 package is one of many R packages for static data visualization. CRAN hosts thousands of packages designed for a wide range of data-related tasks.
You can explore and discover more packages by browsing the official CRAN package repository.
Note: Learn about differences between R and Python.
Conclusion
You have installed R on Ubuntu and can use its functions and environment to extract valuable insights from complex datasets.
It is common for researchers to use R in tandem with Python libraries like pandas. By combining R and Python, you can take advantage of the vast range of packages and functionalities available in both ecosystems.