Data Analysis and Visualisations using R
By: Samarth Malik
Tags: R Programming, Analytics, Titanic Dataset, Data Science, Data Visualization

Data Analysis and Visualisations using R

Step by Step Guide for Beginners

Are you starting your journey in the field of Data Science? Do you need to know how to get started with R? Are you intrigued by Data Visualisations? If yes, then this tutorial is meant for you!

Overview & Purpose

With this article, we’d learn how to do basic exploratory analysis on a data set, create visualisations and draw inferences.

What we’d be covering

Getting Started with R Understanding your Data Set Analysing & Building Visualisations

1. Getting Started with R

1.1 Download and Install R | R Studio

R programming offers a set of inbuilt libraries that help build visualisations with minimal code and flexibility.

You can download R easily from the R Project Website. While downloading you would need to choose a mirror. Choose R depending on your operating system, such as Windows, Mac or Linux.

It is super easy to install R. Just follow through the basic installation steps and you’d be good to go.

For an easy way to write scripts, I recommend using R Studio. It is an open source environment which is known for its simplicity and efficiency.

Launch Screen after starting R Studio

1.2 Install R packages

Packages are the fundamental units created by the community that contains reproducible R code. These include reusable R functions, documentation that describes how to use them and sample data.

The directory where packages are stored is called the library. R comes with a standard set of packages. Others are available for download and installation. Once installed, they have to be loaded into the session to be used.

To install a package in R, we simply use the command

install.packages(“Name of the Desired Package”)

1.3 Loading the Data set

There are some data sets that are already pre-installed in R. Here, we shall be using The Titanic data set that comes built-in R in the Titanic Package.

While using any external data source, we can use the read command to load the files(Excel, CSV, HTML and text files etc.)

This data set is also available at Kaggle. You may download the data set, both train and test files. In this tutorial, we’d be just using the train data set.

titanic <- read.csv(“C:/Users/Desktop/titanic.csv”, header=TRUE, sep=”,”)

The above code reads the file titanic.csv into a dataframe titanic. With Header=TRUE we are specifying that the data includes a header(column names) and sep=”,” specifies that the values in data are comma separated.

2. Understanding the Data set

We have used the Titanic data set that contains historical records of all the passengers who on-boarded the Titanic. Below is a brief description of the 12 variables in the data set :

PassengerId: Serial Number

Survived: Contains binary Values of 0 & 1. Passenger did not survive — 0, Passenger Survived — 1.

Pclass — Ticket Class | 1st Class, 2nd Class or 3rd Class Ticket

Name — Name of the passenger

Sex — Male or Female

Age — Age in years — Integer

SibSp — No. of Siblings / Spouses — brothers, sisters and/or husband/wife

Parch — No. of parents/children — mother/father and/or daughter, son

Ticket — Serial Number

Fare — Passenger fare

Cabin — Cabin Number

Embarked — Port of Embarkment | C- Cherbourg, Q — Queenstown, S — Southhampton

2.1 Peek at your Data

Before we begin working on the dataset, let’s have a good look at the raw data.

view(titanic)

This helps us in familiarising with the data set.

head(titanic,n) | tail(titanic,n)

In order to have a quick look at the data, we often use the head()/tail().

Top 10 rows of the data set.

Bottom 5 rows of the data set.

In case we do not explicitly pass the value for n, it takes the default value of 5, and displays 5 rows.

names(titanic)

This helps us in checking out all the variables in the data set.

Familiarising with all the Variables/Column Names

str(titanic)

This helps in understanding the structure of the data set, data type of each attribute and number of rows and columns present in the data.

summary(titanic)