Introduction To R: Session 6
Introduction To R: Session 6
1
WHILE LOOP
2
R-WHILE LOOP
The While loop executes the same code again and again until a
stop condition is met. the condition part of this recipe should
become FALSE at some point during the execution. Otherwise,
the while loop will go on indefinitely.
Syntax
The basic syntax for creating a while loop in R is −
7
# Initialize the speed variable
speed <- 88
8
FOR LOOP
This for loop consists of the following parts:
for(i in values){
✓ The keyword for, followed by parentheses.
. . . do something . . .
✓ An identifier between the parentheses. In this example, we use i, but
}
that can be any object name you like.
✓ The keyword in, which follows the identifier.
✓ A vector with values to loop over. In this example code, we use the
object values, but that again can be any vector you have available.
✓ A code block between braces that has to be carried out for every value
in the object values
9
TO PRINT VALUES FROM 1-10
for(i in 1:10) [1] "Values of i= 1"
11
print("Repeat Statement")
REPEAT i<-1
repeat
{
Loop variable initialization
print(paste("i=",i))
repeat
i=i+1
{
if(i==10)
statements
{
Loop variable increment/decrement
break ()
Break statement
}
}
}
print("the repeat loop breaks the loop
after 10")
12
GRAPHICS
13
STARTING OUT WITH GRAPHS
# Creating a Graph
attach(mtcars)
plot(wt, mpg)
abline(lm(mpg~wt))
title("Regression of MPG on Weight")
15
BAR PLOTS
H <- c(7,12,28,3,41)
M <- c("Mar","Apr","May","Jun","Jul")
16
EXERCISE:
1. Write a R program to create a simple bar plot of five subjects marks.
22
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 −
25
LINE CHART
plot(v,type,col,xlab,ylab)
Following is the description of the parameters used −
v is a vector containing the numeric values.
type takes the value "p" to draw only the points, "l" to draw only the lines and "o" to
draw both points and lines.
xlab is the label for x axis.
ylab is the label for y axis.
main is the Title of the chart.
col is used to give colors to both the points and lines
26
LINE PLOT
# Create the data for the chart.
v <- c(7,12,28,3,41)
# Plot the bar chart.
plot(v,type = "o")
27
LINE PLOT
v <- c(7,12,28,3,41)
t <- c(14,7,6,19,3)
# Plot the bar chart.
plot(v,type = "o",col = "red", xlab = "Month", ylab = "Rain fall", main = "Rain fall chart")
lines(t, type = "o", col = "blue")
29
SCATTER PLOTS
Scatterplots show many points plotted in the Cartesian plane. Each point represents the
values of two variables. One variable is chosen in the horizontal axis and another in the
vertical axis.
The basic syntax for creating scatterplot in R is −
plot(x, y, main, xlab, ylab, xlim, ylim, axes)
Following is the description of the parameters used −
x is the data set whose values are the horizontal coordinates.
y is the data set whose values are the vertical coordinates.
main is the tile of the graph.
xlab is the label in the horizontal axis.
ylab is the label in the vertical axis.
xlim is the limits of the values of x used for plotting.
ylim is the limits of the values of y used for plotting.
axes indicates whether both axes should be drawn on the plot
30
SCATTER PLOT
31
PIE CHARTS
Pie chart is drawn using the pie() function in R programming . Pie charts are created
with the function pie(x, labels=) where x is a non-negative numeric vector indicating the
area of each slice and labels= notes a character vector of names for the slices
items<-c("housing", "food", "clothes", "rent", "other")
expenses<-c(600,300,150,100,200)
pie(expenses, labels=items)
32
SIMPLE PIE CHART
# Simple Pie Chart
slices <- c(10, 12,4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pie(slices, labels = lbls, main="Pie Chart of Countries")
33
PIE CHART WITH
PERCENTAGES
# Pie Chart with Percentages
slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
pie(slices,labels = lbls, col=rainbow(length(lbls)),
main="Pie Chart of Countries")
34
3-D PIE CHART
# 3D Exploded Pie Chart
library(plotrix)
slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany",
"France")
pie3D(slices,labels=lbls,explode=0.1,
main="Pie Chart of Countries ")
35
R LAB Practice
36
EXERCISE 1
Write a R program to take input from the user (name and gender) and display the
values. Also print the version of R installation.
Source causes R to accept its input from the named file or URL or connection or expressions directly. ... Its main
purpose is to evaluate and auto-print expressions as if in a top level context, e.g, as in the R console.
SOLUTION EXERCISE 1
Write a R program to take input from the user (name and gender) and display the
values. Also print the version of R installation.
SOLUTION- EXERCISE 4
fibbo(n)
print("Fibonaci Series")
a<-1
b<-1
fibbo<-function (n)
{
while(n>0)
{
s<-a+b
print(s)
a=b
b=s
n<-n-1