Category Archives: R Tutorial

R Tutorial

R Inheritance

R Inheritance

The inheritance is one of the important feature in object oriented programming, that facilitates the code re-usability. Inheritance is a mechanism through which a class(derived class) can inherit properties and methods from an existing class(base class).

Sub Class:- The class that inherits properties and methods from another class is know as Sub-class or Derived-Class.

Super Class:- The class whose properties and methods are being inherited by sub class is called Base Class or Super class.

Inheritance in S3 Class

Inheritance in S4 Class

Inheritance in Reference Class

R Reference Class

R Reference Class

Reference class is basically a S4 class with an environment added to it.

Defining a Reference class

In R, reference classes are defined using the setRefClass() function.

Syntax:-

Example:-

Defining reference objects

Reference Methods

R Object and Class

R Object and Class

Class can be defined as a blueprint or prototype of associated objects. Class wrapper that binds the data and methods which can be later accessed by objects of that class. We can think of a class as a user-defined data type, that describes the behavior and characteristics shared by all of its instances. Once a class has been defined, we can create instance or objects of that class which has access to class properties and methods.

R S3 Class

R S4 Class

R Reference Class

R Data Reshaping

R Data Reshaping

In R, before we perform any analysis over data provided, it must be changed it into required format. Data Reshaping is the process about changing the way data is organized into rows and columns. In data reshaping we take input data as data frame and change or reshape it into required format. Sometimes we may need to convert input data into intermediate format in order to reshape it in required data format.

R comes with a rich set of functions for reshaping data prior to analysis including functions to split, merge and change the rows to columns and vice-versa in a data frame.

R Transpose a Matrix

The t() function is used to transpose a matrix or a data frame.

Example:-

Output:-

r_transpose_matrix

The Reshape Package

In R, we have a “reshape” package which includes a rich set of data reshaping function that makes it easy to perform data analysis.

Melting and Casting

Reshaping of data may involve multiple steps in order to convert input data into required format. We generally melt data so that each row is converted into unique id-variable combination. Then we cast this data into desired format. The functions used to do this are melt() and cast().

Melt Data using melt() function

Example:-

Lets take a sample data set as following –

id point x1 x2
1 1 5 6
1 2 3 5
2 1 6 1
2 2 2 4

Now, lets melt the above data set using melt() function, melt() function will convert all columns other than id and point into multiple rows.

Output:-

id point variable value
1 1 x1 5
1 2 x1 3
2 1 x1 6
2 2 x1 2
1 1 x2 6
1 2 x2 5
2 1 x2 1
2 2 x2 4

Cast Data using cast() function

Now, lets cast the melted data to evaluate mean –

Output:-

idmeans

id x1 x2
1 4 5.5
2 4 2.5

pointmeans

point x1 x2
1 5.5 3.5
2 2.5 4.5

Merging Data Frames

In R, merge() function can be used to merge two data frames. In order to merge data frames they must have same column names on which merging is performed. The merge function is mostly used to merge data frames horizontally. Mostly, merging is performed on one or more common key variables (i.e., an inner join).

Example:-

Joining Vectors using cbind() function

In R, cbind() function is used to join multiple vectors into data frame.

Syntax:-

dfA

Type Sex Exp
B M 50
A F 15

dfB

Age City
30 NY
20 SF

Example:-

Output:-

Type Sex Exp Age City
B M 50 30 NY
A F 15 20 SF

Joining Data Frame using rbind() function

In R, rbind() function is can be used to join multiple data frame.

dfA

Type Sex Exp
B M 50
A F 15

dfB

Type Sex Exp
D F 10
C M 5

Example:-

Output:-

Type Sex Exp
B M 50
A F 15
D F 10
C M 5

R Packages

R Packages

Package is simply a set of R functions organized in an independent, reusable unit. It usually contains set of functions for a specific purpose or utility along with the complied code and sample data. All of the R packages are stored in library directory. Every R package has its own context, thus it does not interfere with other modules.

R comes with a rich set of default packages, loaded automatically when R console is started. However, any other package other than the default need to be installed and loaded explicitly first in order to use it. Once a packages is loaded, it can be used throughout the R environment.

Check Package Directory Path

The .libPaths() function is used to show package directory path.

Example:-

Output:-

r_show_package_directory

List R Packages

The library() function is used to display list of all of the installed package.

Syntax:-

Output:-

The search() function is used to display list of all of the packages currently loaded in the R environment.

Syntax:-

Output:-

Installing R Package

In R, there are two ways we can install a new package.

Install using CRAN:-

CRAN is a distributed network around the world that store identical, up-to-date versions of a package code and documentation, which allows you to install a packages from nearest CRAN mirror.Use the following commands to install a R package directly from CRAN mirror –

Install Manually:-

Step 1:- Download the R Package directly from the following link –

Step 2:- Save package zip file in your local system.

Step 3:- Install the package using following commands

Load Package

In R, all of the default packages are loaded automatically when R console is started. However, any other package other than the default need to be installed and loaded explicitly first in order to use it. Once a packages is loaded, it can be used throughout the R environment. In R, a package can be loaded using following commands –

Updating Package

Once a package is installed, you may frequently require to update them in order to have an latest package code. A package can be updated using update.packages command as following –

Delete Package

Sometime you may require to delete any package, this can be done using the remove.packages() function as following –

R Data Frames

R Data Frames

Data Frame is a data structure that contain list of vectors that is of equal length. Vectors in a data frame can be different data types i.e. numeric, character, factor, etc. It is a two dimensional data structure that us mainly used to store data tables.

Example:-

Here, df is a data frame containing three vectors a, b, c. When we run the above R script, we see the following output –

Output:-

r_data_frame_example

In Data Frame, each of the component form column and its contents form the rows.

Creating a Data Frame

In R, a data frame is created using data.frame() function. The data.frame() function takes a vector input in order to create a factor.

Syntax:-

Above is the general syntax of data.frame() function, here

df:- It is collection of vector input that us being join as data frame, or it could be a matrix that can be converted to data frame.
stringsAsFactors:- It convert string to factor by default. To suppress this behavior, we can pass assign FALSE to it.

Let’s create a data frame using data.frame() function –

Example:-

Here, df is a data frame containing three vectors a, b, c. When we run the above R script, we see the following output –

Output:-

r_data_frame_example

Accessing factor Elements

Elements in a data frame can be accessed same way as of list or matrix by passing row and column index value(s) in brackets [ ] separated with a comma you can access individual data frame element. However, individual column can be accessed using [[ or $ operator.

Syntax:-

df:- Data frame name
row:- row index of the element, if not provided specified row elements will be fetched for all columns.
col:- column index of the element, if not provided specified column elements will be fetched for all rows.

Example:-

When we run the above R script, we see the following output –

Output:-

r_accessing_data_frame

Modify Data frame Element

A data frame element can be modified by accessing element using above method and assigning it new value.

Example:-

Output:-

r_modify_data_frame

Data Frame rbind() and cbind() Function

The rbind() and cbind()function is used to add a row and column respectively to a data frame.

Example:-

Output:-

r_data_frame_rbind_cbind

Delete Data frame Element

A data frame element can be deleted by simply accessing and assigning NULL value to it.

R Factors

R Factors

Factor is a data structure that can only contain predefined set of values(categorical data). Factors are useful when we want deal with limited set of values for variable, in this case we must define the possible values beforehand and all of the distinct values are termed as levels.

Example:-

Here, we have defined a factor that contains all of 7 weekdays as values.

Output:-

When we run the above R script, we see the following output –

It list all of the factor values and levels. It has the 7 levels.

Creating a factor

In R, a factor is created using factor() function. The factor() function takes a vector input in order to create a factor.

Syntax:-

Above is the general syntax of factor() function, here

data:- It is a vector input which contains all of the factor values.
levels:- It takes a vector input(lData) which contains all of the factor levels. Levels can be inferred from the data if not provided.

Example:-

Output:-

r_creating_factor

Accessing factor Elements

Factor elements can be accessed same way as of vectors, by passing index value(s) in brackets [ ] you can access factor elements. An index value can be logical, integer or character.

Example:-

Output:-

r_accessing_factor_elements

Modify Factor Element

A factor element can be modified by accessing element and assigning it new value from predefined set of values.

Example:-

Output:-

r_modify_factor_element

Note:- If we try to assign values outside of its predefined levels, then it raise an error.

R Arrays

R Arrays

Array is a data structure that enable you to store multi dimensional(more than 2 dimensions) data of same type. In R, an array of (2, 3, 2) dimension contains 2 matrix of 2×3.

Creating Arrays

In R, an array can be created using the array() function as following –

Syntax:-

Above is the general syntax of array() function, here

data:- It is a vector input used to create array.
dim:- It is used to represent array dimensions.

Example:-

Output:-

r_arrays

Naming Columns and Rows

The dimnames parameter in array() function is used to give names to the rows, columns along with matrices.

Example:-

Output:-

r_naming_arrays

Accessing Array Elements

Array elements can be accessed by using the row and column index of the element as following –

Syntax:-

arr:- Array name
row:- row index of the element, if not provided specified row elements will be fetched for all columns.
col:- column index of the element, if not provided specified column elements will be fetched for all rows.
matrix:- matrix index.

Example:-

When we run the above R script, we see the following output –

Output:-

r_accessing_array_elements

R Matrix

R Matrix

Matrix is a two dimensional data structure, where elements are arranged in rows and columns. It can thought as combination of two or more vectors of same data type. Following is an example of a 2×4 matrix with 2 rows and 4 columns.

r_matrix_example

Creating a Matrix

A matrix is created using the matrix() function. The values for matrix rows and matrix columns is defined using nrow and ncol arguments. However, it is not required to provide value for both as it automatically evaluate the value for other dimension using length of matrix.

Syntax:-

Above is the general syntax of matrix() function, here

data:- It is a vector input which will arranged as matrix.
nrow:- Number of rows.
ncol:- Number of columns.
byrow:- If it is TRUE values being filled by row (left to right). If it is FALSE values being filled by Column(top to bottom). Default is FALSE.

Example:-

Here we are creating two 5×2 matrix which contains number from 1 to 10 keeping byrow = TRUE for and byrow = FALSE for other to check the difference.

Output:-

r_creating_matrix

Accessing Matrix Element

Matrix elements can be accessed by using the row and column index of the element as following –

Syntax:-

matrix:- Matrix name
row:- row index of the element, if not provided specified row elements will be fetched for all columns.
col:- column index of the element, if not provided specified column elements will be fetched for all rows.

Example:-

Let’s have a 3×5 matrix which contains number from 1 to 15 keeping byrow = TRUE. Now, we are accessing different matrix elements as following –

When we run the above R script, we see the following output –

Output:-

r_access_matrix_elements

Update Matrix Element

A matrix element can be modified by accessing element using above method and assigning it new value.

Example:-

Output:-

r_update_matrix_element

Matrix rbind() and cbind() Function

The rbind() and cbind()function is used to add a row and column respectively to a matrix.

Example:-

Output:-

r_matrix_cbind_rbind

R Transpose a Matrix

The t() function is used to transpose a matrix.

Example:-

Output:-

r_transpose_matrix

R Lists

R Lists

List is a basic data structure that can contain elements of different types. It is similar to vectors except it can contains mixed data i.e. numbers, strings, vectors and another list inside it.

Example:-

Creating a List

A list can be created using list() function as following –

Example:-

Following is an example to create a list containing strings, numbers, vectors and a logical values.

Output:-

r_creating_list

Naming List Elements

In R, we can assign name or tags to the list elements, that makes it easy to reference the individual element of the list.

Example:-

Following is an example to create same list with the tags as follows.

Here,

a, b and c are tags which makes it easier to reference the respective list element.

Accessing List Elements

List elements can be accessed in same as of vectors. An index value can be logical, integer or character.

Example:-

Output:-

r_accessing_list_elements

Add Element To List

An element can be simply added by assigning a values with new tags.

Example:-

Here, we have added a new list element with “d” tag and assigning it value “w3adda”. When we run the above R script, we will see following output –

Output:-

r_add_list_element

Update List Element

A list element can be modified by accessing element and assigning it new value.

Example:-

Here, we have updated list element with index position 1 with new value. When we run the above R script, we will see following output –

Output:-

r_update_list_element

Delete Element From List

An element can be simply deleted by assigning NULL value to it.

Example:-

Here, we have deleted list element with index position 1 by assigning it NULL. When we run the above R script, we will see following output –

Output:-

r_delete_list_element

Merging Lists

In R, we can create a new list by merging multiple list using list() function as following –

Example:-

Here, we have two list list1 and list2, and we have merge both of the list into list3. When we run the above R script, we will see following output –

Output:-

r_merge_list

Converting List to Vector

The unlist() function can be used to convert a list object into a vector.

Example:-

Output:-

r_convert_list_to_vector

R String tolower

R String tolower

The tolower() function is used to convert given string characters to lower case.

Syntax:-

Above is the general syntax of tolower() function, here

str:- It is a vector input representing a string.
Example:-

Output:-

r_string_tolower