Statistical Computing and Graphics in R

# R Language Basics

## Using R as a Calculator

In the Windows Operating system, The R installer will have created an icon for R on the desktop and a Start Menu item. Double-click the R icon to start the R Program; R will open the console, to type R commands.

The greater than sing (>) in the console is the prompt symbol. In this tutorial, we will use the R language as a calculator (we will be Using R as a Calculator for mathematical expressions), by typing some simple mathematical expressions at the prompt (>). Anything that can be computed on a pocket calculator can also be computed at the R prompt. After entering the expression on the prompt, you have to press the Enter key from the keyboard to execute the command.

### Using R Language As a Calculator

Some examples using R as a calculator are as follows

> 1 + 2   #add two or more numbers
> 1 - 2   #Substracts two or more numbers
> 1 * 2   #multiply two or more numbers
> 1 / 2   #divides two more more numbers
> 1 %/% 2 #gives the integer part of the quotient
> 2 ^ 1   #gives exponentiation
> 31 %% 7 #gives the remainder after division

These operators also work fine for complex numbers.

Upon pressing the enter key, the result of the expression will appear, prefixed by a number in a square bracket:

> 1 + 2
[1] 54

The [1] indicates that this is the first result from the command.

One can also use R as an advanced scientific calculator. Some advanced calculations that are available in scientific calculators can also be easily done in R for example,

> sqrt(5)      #Square Root of a number
> log(10)      #Natural log of a number
> sin(45)      #Trignometric function (sin function)
> pi           #pi value 3.141593
> exp(2)       #Antilog, e raised to a power
> log10(5)     #Log of a number base 10
> factorial(5) #Factorial of a number e.g 5!
> abs(1/-2)    #Absolute values of a number
> 2*pi/360     #Number of radian in one Babylonian degree of a circle

Remember R prints all very large or very small numbers in scientific notation.

### Order of Precedence/ Operations

The R language also makes use of parentheses for grouping operations to follow the rules for the order of operations. for example

> 1 - 2/3   #It first computes 2/3 and then subtracts it from 1
> (1-2)/3   #It first computes (1-2) and then divides it by 3

The R Language recognizes certain goofs, like trying to divide by zero, missing values in data, etc.

> 1/0   #Undefined, R tells it an infinity (Inf)
> 0/0   #Not a number (NaN)
> "one"/2   #Strings or characters is divided by a number

Further Reading: Computing Descriptive Statistics in R

Online MCQs Computer Science with Answers

## Introduction to R Language

#### What is R (Language)

R is an open-source (GPL) programming language for statistical computing and graphics, made after S and S-plus language. The S language was developed by AT&T laboratories in the late ’80s. Robert Gentleman and Ross Ihaka started the research project of the statistics department of the University of Auckland in 1995 called R Language.

The R language is currently maintained by the R core development team (an international team of volunteer developers). The (R Project website) is the main site for information about R. From this page information about obtaining the software, accompanying package, and many other sources of documentation (help files) can be obtained.

R provides a wide variety of statistical and graphical techniques such as linear and non-linear modeling, classical statistical tests, time-series analysis, classification, multivariate analysis, etc., as it is an integrated suite of software having facilities for data manipulation, calculation, and graphics display. It includes

• Effective data handling and storage facilities
• Have a suite of operators for calculation on arrays, particularly for matrices
• Have a large, coherent, integrated collection of intermediate tools for data analysis
• Graphical data analysis
• Conditions, loops, user-defined recursive functions, and input-output facilities.

#### Obtaining R Software

R program can be obtained/downloaded from the R Project site the ready-to-run (binaries) files for several operating systems such as Windows, Mac OS X, Linux, Solaris, etc. The source code for R is also available for download and can be compiled for other platforms. R language simplifies many statistical computations as R is a very powerful statistical language having many statistical routines (programming code) developed by people from all over the world and are freely available from the R project website as “Packages”. The basic installation of R language contains many powerful sets of tools and it includes some basic packages required for data handling and data analysis.

Many users of R think of R as a statistical system, but it is an environment within which statistical techniques are implemented. The R language can also be extended via packages.

#### Installing R

For Windows, the operating system binary version is available from http://cran.r- project.org/bin/windows/base/. “R-3.0.0-win.exe. R-3.0.0” is the latest version of R released on 03 April 2013 by Duncan Murdoch.
After downloading the binary file double-click it, and almost automatic installation of the R system will start although the customized installation option is also available. Follow the instructions during the installation procedure. Once the installation process is complete, you have the R icon on your computer desktop.

#### The R Console

When R starts, you will see R console windows, where you type commands to get the required results. Note that commands are typed on the R Console command prompt. You can also edit the commands previously typed on the command prompt by using the left, right, up, and down arrow keys, home, end, backspace, insert, and delete keys from the keyboard. Command history can be obtained by up and down arrow keys to scroll through recent commands. It is also possible to type commands in a file and then execute the file, using the source function in the R console.

Books on R Programming Language

The following books can be useful for learning the R and S language.

• “Psychologie statistique avec R” by Yvonnick Noel. Partique R. Springer, 2013.
• “Instant R: An introduction to R for Statistical Analysis” by Sarah Stowell. Jotunheim Publishing, 2012.
• “Financial Risk Modeling and Portfolio Optimization with R” by Bernhard Pfaff. Wiley, Chichester, UK, 2012.
• “An R Companion to Applied Regression” by John Fox and Sanford Weisberg, Sage Publications, Thousand Oaks, CA, USA, 2nd Edition, 2011,
• “R Graphs Cookbook” by Hrishi Mittal, Packt Publishing, 2011
• “R in Action” by Rob Kabacoff. Manning, 2010.
• “The Statistical Analysis with R Beginners Guide” by John M. Quick. Packt Publishing, 2010.
• “Introducing Monte Carlo Methods with R” by Christian Robert and George Casella. Use R. Springer, 2010.
• “R for SAS and SPSS users” by Robert A. Muenchen. Springer Series in Statistics and Computing. Springer, 2009.

## R Language: A Quick Reference – II

R language: A Quick Reference is about learning R Programming with a short description of the widely used commands. It will help the learner and intermediate user of the R Programming Language to get help with different functions quickly. This Quick Reference is classified into different groups. Let us start with R Language: A Quick Reference – II.

This R Language: A Quick Reference contains R commands about creating vectors, matrices, lists, data frames, arrays, and factors. It also discusses setting the different properties related to R language data types.

### Creating Vectors in R Language

The creation of a row or column vector in the R Language is very important. One can easily create a vector of numbers, characters/ strings, complex numbers, and logical values, and can concatenate the elements.

### Creating Lists in R Language

Creating Lists in R is important as it can store different types of data and even lists. A vector can also be used to create a list of $k$ elements.

### Creating Matrices in R Language

Two-dimensional data can be created using the matrix command in R.

### Creating Factors in R Language

To create categorical variables, R has a concept of factors as variables. All factors have levels that may have ordered factors.

### Creating a Data Frame in R Language

A data frame is a tabular data format used for statistical data analysis. The format of the data is like data entered in spreadsheets for data analysis.

### R Language Data Type Properties

Every data object has different properties. These properties can be used to find out the number of rows in a vector or matrix, the number of columns, names of rows and columns of a matrix or data frame.

## R Language: A Quick Reference – I

R language: A Quick Reference is about learning R Programming with a short description of the widely used commands. It will help the learner and intermediate user of the R Programming Language to get help with different functions quickly. This Quick Reference is classified into different groups. Let us start with R Language: A Quick Reference – I.

### Basic Data Representation

In the R Language, data values or data may be represented as logical values (Such as True, or False), in scientific notation, as a complex, or as a float number. The are some certain values such as NA, NULL, NaN, and Inf values.

### Checking/ Testing the Basic Data Types

The type of data can be checked using some functions such as is.logical(), is.numeric(), is.list(), is.character(), is.vector() or is.complex() function.

### Checking/ Testing the Special Values

The type of special values can be checked using is.na(), is.nan(), is.finite(), is.ordered(), and is.factor() etc., functions

### Changing Basic Data Type

The type of data can be changed by using functions such as, as.logical(), as.numeric(), as.list(), or as.numeric() etc., functions.

### Basic Mathematical Operations

R can be used as a calculator. The mathematical operations such as addition, subtraction, multiplication, and division can also be performed.

## Rounding off the Numbers

The numbers or values of a variable can be rounded as desired.

### Common Mathematical Functions

The commonly used mathematical functions in R language are abs(), sqrt(), exp(), log(), and different bases of log functions.

### Trigonometric and Hyperbolic Functions

Following is the list of different trigonometric and Hyperbolic functions

### Special Mathematical Functions

The following is the list of special mathematical functions.