Home » R9 May 2017

Basic Introduction to R Programming [Beginner Tutorial]

This article aims to introduce the very basics of R programming. This artilce includes R programming introduction, installation, basic syntax, running code, data types, relational operators and functions.

1. Introduction

1.1 What is it?

– Programming Language Software
– Used for Statistical Analysis & Graphical Representation
– Freely available

1.2 What can it do?

– Data Analysis / Data Mining
– Data Visualization
– Data Reporting

2. Basic Coding

2.1 Installation

Here’s the list of R download package for Windows, Linux & Mac OS: https://cran.r-project.org/bin/

For Linux, you can also install R via command line. Here’s an example of installing R on Ubuntu Linux:

Once R is installed, you can run it from terminal by running “R” command.

To run R in GUI version, you need to download and install R-Studio from https://www.rstudio.com/products/rstudio/download

2.2 Basic Syntax

The operator <- can be used anywhere, whereas the operator = is only allowed at the top level.

Running R file:

3. Data Types (R-Objects)

In R, variables are not declared as data types. R-Objects are assigned to a variable. The data type of the R-Object becomes the data type of the variable. Below are the frequently used R-Objects:

3.1 Vectors

We use c() function which combines elements into vector.

3.2 Lists

List can contain different types of elements within it. A single list can be created with different elements like vectors, another list, integer, string, etc.

3.3 Matrix

A matrix is a two dimensional rectangular dataset. The rows and columns number should be specified while creating the matrix.

3.4 Arrays

Arrays are like matrix. In array, you can specify the dimension of the array. Matrix is two dimensional only but array can be multi-dimensional. In the second example below, we create a 3x3x2 dimensional array.

3.5 Data Frame

Data Frames are tabular data objects. The difference between array & data frame is that each column of a data frame can contain different types of data. For example, we can specifically assign character data for first column and numeric data for second column.

4. Operators

Like other programming languages, R also has different mathematical and logical operators.

4.1 Arithmetic Operators

Used for adding, subtracting, multiplying, dividing, etc. of two or more vectors.

4.2 Relational Operators

Used for comparing two vectors. Each element of first vector is compared with the corresponding element of second vector. The result is in boolean value.

4.3 Logical Operators

Compares two vectors. Each element of first vector is compared with the corresponding element of second vector. The result is in boolean value. All number greater than 1 are considered to be TRUE.

5. Conditional Statements

6. Loops

7. Functions

There are different built-in R functions. You can create your own function as well.

Some of the built-in functions are seq(x), mean(x), median(x), max(x), sum(x), paste(x,y), nchar(x), substring(string, first, last), etc. Here’s a quick reference to different R functions: https://cran.r-project.org/doc/contrib/Short-refcard.pdf

You can create your own custom function as well.

Hope this helps. Thanks.

R

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed

Comments are closed.