R Programming
R Programming
Installing R packages, R – Nuts and Bolts -Getting Data In and Out - Control Structures and Functions-
Loop Functions-Data Manipulation- String Operations- Matrix Operations.R-Charts
BASICS OF R
History of R
Installation of R
2. Run the installer. Default settings are fine. If you do not have admin rights on your
laptop, thenask you local IT support. In that case, it is important that you also ask them
to give you full permissions to the R directories. Without this, you will not be able to
install additional packages later.
Installation of R
Studio
2. Once the installation of R has completed successfully, run the RStudio installer.
3. If you do not have administrative rights on your laptop, step 2 may fail.
Ask your ITSupport or download a pre-built zip archive of RStudio which
doesn’t need installing.
4. The link for this is towards the bottom of the download page, highlighted in Image
5. Download the appropriate archive for your system (Windows/Linux only –
the Macversion can be installed into your personal “Applications” folder
without admin rights).
6. Double clicking on the zip archive should automatically unpack it on most
Windowsmachines.
Installing R packages
Option 1:
Click on the tab ‘ Packages’ then ‘Install’ as shown in figure 1.3. Click
on Install toinstall R packages.
Fig 1.3:Install Packages
Option 2:
Tools -> Install packages. As shown in Fig 1.4.
R is free and open source software, allowing anyone to use and, importantly, to
modify it. R is licensed under the GNU General Public License, with copyright
held by The R Foundation for Statistical Computing.
R has no license restrictions (other than ensuring our freedom to use it at our own
discretion), and so we can run it anywhere and at any time.
Anyone can provide new packages, and the wealth of quality packages available
for R is a testament to this approach to software development and sharing.
R plays well with many other tools, importing data, for example, from CSV files,
SAS, and SPSS, or directly from Microsoft Excel, Microsoft Access, Oracle,
MySQL, and SQLite.
It can also produce graphics output in PDF, JPG, PNG, and SVG formats, and
table output for LATEX and HTML.
write.table() : for writing tabular data to text files (i.e. CSV) or connections
Control Structures in R
1. Conditional
statements.2.Looping
Statements 3.Jump
Statements
Conditional Statements:
Example:If,If-Else,If-ElseIf Ladder,switch
If:
Syntax: If(condition)
{
Statement
Output :
If-Else
Syntax:if(condition)
Statement1
}else{
Statement 2
}
Example:Check for Odd or Even Number
Output:
If-Else if Ladder
Syntax:if(condition)
Statement1
}else if{
Statement 2
}else{
Statement 3
Switch:
A switch statement allows a variable to be tested for equality against a list of values.
Each value is called a case, and the variable being switched on is checked for each case.
Looping Statements in R
There may be a situation when you need to execute a block of code several number of
times. A loop statement allows us to execute a statement or group ofstatements multiple
times.
Example:for,while,Repeat
For:
Description: A For loop is a repetition control structure that allows you to efficiently
write a loop that needs to execute a specific number of times.
Statements
While:
Description: The While loop executes the same code again and again until astop
condition is met.
Syntax:while(condition){
Statement
Output:
Repeat:
Description:The Repeat loop executes the same code again and again until astop
condition is met.
Syntax:
repeat
Commands
If(condition)
Break
Output:
Jump Statements:
Description: Loop control statements change execution from its normalsequence
Example: break, next
Break:
Terminates the loop statement and transfers execution to the statement immediately
following the loop.
Syntax: break
Output:
Next:
Syntax:
next
Functions in R
A function is a set of statements organized together to perform a specific task.R has a
large number of in-built functions and the user can create their own functions.
Syntax:
Function name=function(ar1,ar2,……)
Function body
}
Function Components:
Return Value − The return value of a function is the last expression in the
function body to be evaluated.
Built-in Functions
R has many in-built functions which can be directly called in the program
without defining them first.
Eg: sum.seq,abs,round .
Example:
Output:
User Defined Function:
We can create user-defined functions in R. They are specific to what a user wants and
once created they can be used like the built-in functions. Belowis an example of how
a function is created and used.
Output:
Function with Arguments:
The arguments to a function call can be supplied in the same sequence as defined in
the function or they can be supplied in a different sequence but assigned to the names
of the arguments.
Example:
Output:
We can define the value of the arguments in the function definition and call the
function without supplying any argument to get the default result. But we can also call
such functions by supplying new values of the argument and get non default result.
Example:
Output:
LOOPING FUNCTIONS IN R
lapply:
=Li
st FUN=A
Function
…. =other arguments
Example:
sapply:
=Li
st FUN=A
Function
…. =other arguments
Example:
apply:
X =An Array
mapply:
Example:
Matrices in R
Matrices are the R objects in which the elements are arranged in a two-dimensional
rectangular layout. We use matrices containing numeric elements to be used in
mathematical calculations.
Syntax:
data is the input vector which becomes the data elements of the matrix.
byrow is a logical clue. If TRUE then the input vector elements are
arranged by row.
Matrix Creation:
row.Example:
(ii) Arrange elements sequentially by coloumn.
Matrix Operations:
Various mathematical operations are performed on the matrices using the Roperators.
The result of the operation is also a matrix.The dimensions (number of rows and
columns) should be same for the matrices involved in the operation.
Matrix Addition:
Matrix Subtraction
Matrix Multiplication(Elementwise)
Matrix Multiplication(Real)
Matrix Division:
String Operations in R
String:
Any value written within a pair of single quote or double quotes in R istreated as a string.
Example:S=”Hello”
S1=’hai’
String Manipulation:
Concatenating Strings - paste() function:
Many strings in R are combined using the paste() function. It can takeany number of
arguments to be combined together.
Syntax:
Example
Syntax:
nchar(x)
Example:
Changing the case - toupper() & tolower() functions
Syntax
Example:
Syntax
substring(x,first,last)
These are replacement functions, which replaces the occurrence of asubstring with other
substring.
Example:
grep(value = FALSE) returns an integer vector of the indices of the elements of x that
yielded a match .
Pie Chart
A pie-chart is a representation of values as slices of a circle with different colors. The slices are labeled and
the numbers corresponding to each slice is also represented in the chart.
In R the pie chart is created using the pie() function which takes positive numbers as a vector input. The
additional parameters are used to control labels, color, title etc.
syntax
The basic syntax for creating a pie-chart using the R is −
radius indicates the radius of the circle of the pie chart.(value between
−1 and +1).
clockwise is a logical value indicating if the slices are drawn clockwise oranti
clockwise.
Example:
Output:
3DPie Chart
A pie chart with 3 dimensions can be drawn using additional packages. Thepackage plotrix has a function
called pie3D() that is used for this.
Example:
Output:
BAR CHART
A bar chart represents data in rectangular bars with length of the bar proportional to the value of the
variable. R uses the function barplot() to create bar charts. R can draw both vertical and horizontal bars in
the bar chart. In bar chart each of the bars can be given different colors.
Syntax
The basic syntax to create a bar-chart in R is −
The following script will create and save the bar chart in the current R workingdirectory.
Output:
GroupBar ChartandStackedBar Chart
We can create bar chart with groups of bars and stacks in each bar by usinga matrix as input values.
More than two variables are represented as a matrix which is used to createthe group bar chart and stacked bar
chart.
Example:
Output:
HISTOGRAM
A histogram represents the frequencies of values of a variable bucketed intoranges. Histogram is similar to
bar chat but the difference is it groups the values into continuous ranges. Each bar in histogram represents
the height of the number of values present in that range.
R creates histogram using hist() function. This function takes a vector asan input and uses some more
parameters to plot histograms.
Syntax
The basic syntax for creating a histogram using R is –
hist(v,main,xlab,xlim,ylim,breaks,col,border)
Following is the description of the parameters used −
Example
A simple histogram is created using input vector, label, col and borderparameters.
The script given below will create and save the histogram in the current Rworking directory.
Output:
Example:
Output:
LINE GRAPH
A line chart is a graph that connects a series of points by drawing line segments between them. These points
are ordered in one of their coordinate (usually the x-coordinate) value. Line charts are usually used in
identifying the trends in data.
Syntax
The basic syntax to create a line chart in R is −
plot(v,type,col,xlab,ylab)
type takes the value "p" to draw only the points, "l" to draw only thelines and "o"
to draw both points and lines.
xlab is the label for x axis.
Example
A simple line chart is created using the input vector and the type parameteras "O". The below
script will create and save a line chart in the current R working directory. The features of the
line chart has been expanded by using additional parameters. We add color to the points and
lines, give a title to the chart and add labels to the axes.
Output:
Multiple Lines ina Line Chart
More than one line can be drawn on the same chart by using
the lines()function.
After the first line is plotted, the lines() function can use an additionalvector as input to draw the
second line in the chart,
Example:
Output:
Scattar Plots
Scatterplots show many points plotted in the Cartesian plane. Each point represents the values of two
variables. One variable is chosen in thehorizontal axis and another in the vertical axis.
Syntax
Example
We use the data set "iris" available in the R environment to create a basicscatter plot. Let's use the
columns "Sepal length" and "Sepal Width" in iris.
Example:
Output:
ScatterplotMatrices
When we have more than two variables and we want to find the correlation between one variable versus
the remaining ones we use scatterplot matrix. Weuse pairs() function to create matrices of
scatterplots.
Syntax
The basic syntax for creating scatterplot matrices in R is −
pairs(formula, data)
data represents the data set from which the variables will be taken.
Example
Each variable is paired up with each of the remaining variable. A scatterplot is plottedfor each pair.
Output:
Box Plot
Boxplots are a measure of how well distributed is the data in a data set. It divides the data set into three
quartiles. This graph represents the minimum, maximum, median, first quartile and third quartile in the data
set. It is also useful in comparing thedistribution of data across data sets by drawing boxplots for each of
them.
Syntax
The basic syntax to create a boxplot in R is −
x is a vector or a formula.
varwidth is a logical value. Set as true to draw width of the box proportionate tothe
sample size.
names are the group labels which will be printed under each boxplot.
Example 1:
Output:
Example 2: boxplot(iris$Petal.Length,iris$Petal.Width)Output: