Analytics R Ubuntu

Installing R programming on Ubuntu 18.04

In this lesson, we will take a look at how we can install and start using the R programming language on Ubuntu 18.04. R is an excellent open-source graphical and statistical computing programming language and is one of the most used programming languages after Python for Data Science and Machine Learning, used with one of the best tools, Jupyter Notebooks.

We will start by installing the R programming language on Ubuntu 18.04 and continue with a very simple program in this language. Let’s get started.

Add GPG Keys

We first need to add the relevant GPG keys:

sudo apt-key adv --keyserver --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

Here is what we get back with this command:

Add GPG Keys

Add GPG Keys

Add R Repositories

We can now add R repositories for the R programming language release:

sudo add-apt-repository 'deb bionic-cran35/'

Here is what we get back with this command:

Add R repositories

Add R repositories

Update Package List

Let’s update Ubuntu Package list:

sudo apt update

Install R

We can finally install R programming language now:

sudo apt install r-base

Verify Installation

Run the following command to verify your install:

sudo -i R

We will see the R console once we write above command:

Verify R Installation

Verify R Installation

Start using R programming with Hello World

Once we have an active installation for the R programming language on Ubuntu 18.04, we will start using it in a very simple and traditional “Hello World” program here. To execute a simple program, we can simply open a terminal, type the following command to open R console:

$ R

We can now start writing simple statements in the console now:

> helloLinuxHint <- "Hello World"
> print (helloLinuxHint)

Here is what we get back with this command:

R Hello World

R Hello World

Running R-based scripts

It is also possible to run R-based scripts using the R command line tool. To do this, make a new file ‘linuxhint.R’ with the following content:

helloLinuxHint <- "Hello from the script, World!"

Here is what we get back with a command which will run this script:

Running R program from Rscript

Running R program from Rscript

Here is the command we used:

Rscript linuxhint.R

At last, we will demonstrate another simple program to calculate factorial of a number with R. Here is a sample program which shows how to do this:

num = 5
factorial = 1

# check if the number is negative, positive or zero
if(num < 0) {
  print("Sorry, number cannot be negative.")
} else if(num == 0) {
  print("The factorial of 0 is 1.")
} else {
  for(i in 1:number) {
  factorial = factorial * i
print(paste("The factorial of", num ,"is:",factorial))

We can run the above script with the following command:

Rscript factorial.R

Once we run the provided script, we can see the factorial calculated for a given number:

Calculating factorial of a number

Calculating factorial of a number

Now, you're ready to write your own R programs.

Python vs R for Data Science

If you are a beginner, it is difficult to pick Python or R over each other for data analysis and visualisation. Both of these languages have a lot of common libraries than you can imagine. Almost each and every task can be done in both of these languages, may it be related to data wrangling, engineering, feature selection web scrapping, app and so on. Some points we can consider for Python include:

  • Python is a language to deploy and implement machine learning at a large-scale
  • The code in Python is much more scalable and maintainable
  • Most of the data science job can be done with five Python libraries: Numpy, Pandas, Scipy, Scikit-learn and Seaborn and they have developed majorly over past few hours and is catching up with the R programming language

Some things which make R more useful is the availability of many statistical products which creates excellent output for business use-cases which we will discover in coming posts.

Conclusion: Installing R on Ubuntu 18.04

In this lesson, we studied how we can install and start using the R programming language on Ubuntu 18.04 with very simple programs in the language. This is just a very simple introduction of many lessons to come with the R programming language. Share your feedback for the lesson with me or to LinuxHint Twitter handle.

About the author

Shubham Aggarwal

Shubham Aggarwal

I’m a Java EE Engineer with about 4 years of experience in building quality products. I have excellent problem-solving skills in Spring Boot, Hibernate ORM, AWS, Git, Python and I am an emerging Data Scientist.