0% found this document useful (0 votes)
12 views24 pages

R Programming

Uploaded by

Dr M.Sathya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views24 pages

R Programming

Uploaded by

Dr M.Sathya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

EXPRESSIONS AND DATA STRUCTURES IN R

print("Expressions")
a<-1
b<-4
c<-a^2+2*a*b+b^2
print("The answer for (a+b)2 is :")
c
print("Vector Operations")
print("Numeric Vector")
x<-c(1,2,3,4,5)
x
print("Character Vector")
y<-c('a','b','c')
y
print("String Vector")
z<-c("Deepa","Prabha")
z
print("The length of Vector x")
length(x)
print("The length of Vector y")
length(y)
print("The length of Vector z")
length(z)
print("Accessing the Vector using index")
x[2]
y[3]
z[1]
print("Creating a List from Vectors:")
p<-list(x,y,z)
p
Output

> print("Expressions")
[1] "Expressions"
> a<-1
> b<-4
> c<-a^2+2*a*b+b^2
> print("The answer for (a+b)2 is :")
[1] "The answer for (a+b)2 is :"
>c
[1] 25
> print("Vector Operations")
[1] "Vector Operations"
> print("Numeric Vector")
[1] "Numeric Vector"
> x<-c(1,2,3,4,5)
>x
[1] 1 2 3 4 5
> print("Character Vector")
[1] "Character Vector"
> y<-c('a','b','c')
>y
[1] "a" "b" "c"
> print("String Vector")
[1] "String Vector"
> z<-c("Deepa","Prabha")
>z
[1] "Deepa" "Prabha"
> print("The length of Vector x")
[1] "The length of Vector x"
> length(x)
[1] 5
> print("The length of Vector y")
[1] "The length of Vector y"
> length(y)
[1] 3
> print("The length of Vector z")
[1] "The length of Vector z"
> length(z)
[1] 2
> print("Accessing the Vector using index")
[1] "Accessing the Vector using index"
> x[2]
[1] 2
> y[3]
[1] "c"
> z[1]
[1] "Deepa"
> print("Creating a List from Vectors:")
[1] "Creating a List from Vectors:"
> p<-list(x,y,z)
>p
[[1]]
[1] 1 2 3 4 5
[[2]]
[1] "a" "b" "c"
[[3]]
[1] "Deepa" "Prabha"
>
2. MANIPULATION OF VECTORS AND MATRIX

print(" Creating Vectors")


a<-c(1,3,4,5,2,6,5,4,2)
b<-c(1,2,2,1,2,3,2,1,2)
a
b
print("Creating Matrix from Vectors")
A<-matrix(a,nrow=3,ncol=3,byrow=TRUE)
print("Matrix A")
A
B<-matrix(b,nrow=3,ncol=3,byrow=3)
print("Matrix B")
B
print("Matrix Addition")
C=A+B
rownames(C)=c("2022","2024","2025")
colnames(C)=c("Rice","Wheat","Maize")
C
print("Matrix Multiplication")
D=A%*%B
rownames(D)=c("2022","2024","2025")
colnames(D)=c("Rice","Wheat","Maize")
D
print("Matrix Division")
E=A/B
rownames(E)=c("2022","2024","2025")
colnames(E)=c("Rice","Wheat","Maize")
E
Output

> print(" Creating Vectors")


[1] " Creating Vectors"
> a<-c(1,3,4,5,2,6,5,4,2)
> b<-c(1,2,2,1,2,3,2,1,2)
>a
[1] 1 3 4 5 2 6 5 4 2
>b
[1] 1 2 2 1 2 3 2 1 2
> print("Creating Matrix from Vectors")
[1] "Creating Matrix from Vectors"
> A<-matrix(a,nrow=3,ncol=3,byrow=TRUE)
> print("Matrix A")
[1] "Matrix A"
>A
[,1] [,2] [,3]
[1,] 1 3 4
[2,] 5 2 6
[3,] 5 4 2
> B<-matrix(b,nrow=3,ncol=3,byrow=3)
> print("Matrix B")
[1] "Matrix B"
>B
[,1] [,2] [,3]
[1,] 1 2 2
[2,] 1 2 3
[3,] 2 1 2
> print("Matrix Addition")
[1] "Matrix Addition"
> C=A+B
> rownames(C)=c("2022","2024","2025")
> colnames(C)=c("Rice","Wheat","Maize")
>C
Rice Wheat Maize
2022 2 5 6
2024 6 4 9
2025 7 5 4
> print("Matrix Multiplication")
[1] "Matrix Multiplication"
> D=A%*%B
> rownames(D)=c("2022","2024","2025")
> colnames(D)=c("Rice","Wheat","Maize")
>D
Rice Wheat Maize
2022 12 12 19
2024 19 20 28
2025 13 20 26
> print("Matrix Division")
[1] "Matrix Division"
> E=A/B
> rownames(E)=c("2022","2024","2025")
> colnames(E)=c("Rice","Wheat","Maize")
>E
Rice Wheat Maize
2022 1.0 1.5 2
2024 5.0 1.0 2
2025 2.5 4.0 1
>
3. OPERATIONS ON FACTORS IN R

print("Creating a Vector")
news<-c("North","South","West","East","South","East","North","West","East","North")
news
print("Converting the Vector into Factor")
a<-factor(news)
a
print("Check whether the Factor is true or false")
print([Link](a))
print("Accessing the factor value using index")
print(a[4])
print(a[7])
print(a[10])
print("Accessing Multiple Elements")
print(a[c(4,6,8)])
print("Replacing the values in a Factor")
a[1]="South"
a
print("Deleting the values in a Factor")
print(a[-4])
Output
> print("Creating a Vector")
[1] "Creating a Vector"
> news<-
c("North","South","West","East","South","East","North","West","East","North")
> news
[1] "North" "South" "West" "East" "South" "East" "North" "West" "East"
[10] "North"
> print("Converting the Vector into Factor")
[1] "Converting the Vector into Factor"
> a<-factor(news)
>a
[1] North South West East South East North West East North
Levels: East North South West
> print("Check whether the Factor is true or false")
[1] "Check whether the Factor is true or false"
> print([Link](a))
[1] TRUE
> print("Accessing the factor value using index")
[1] "Accessing the factor value using index"
> print(a[4])
[1] East
Levels: East North South West
> print(a[7])
[1] North
Levels: East North South West
> print(a[10])
[1] North
Levels: East North South West
> print("Accessing Multiple Elements")
[1] "Accessing Multiple Elements"
> print(a[c(4,6,8)])
[1] East East West
Levels: East North South West
> print("Replacing the values in a Factor")
[1] "Replacing the values in a Factor"
> a[1]="South"
>a
[1] South South West East South East North West East North
Levels: East North South West
> print("Deleting the values in a Factor")
[1] "Deleting the values in a Factor"
> print(a[-4])
[1] South South West South East North West East North
Levels: East North South West
>
4. DATA FRAMES IN R

print("Creating Vectors")
Name<-c("Rekha","Divya","Gokul")
Maths<-c(46,78,59)
Science<-c(67,45,56)
print("Creating Data Frames from Vectors")
a<-[Link](Name,Maths,Science)
a
print("To find the Dimension of the Data Frame")
dim(a)
print("Accessing values with Name")
a["Name"]
a["Maths"]
print("Adding a New Column")
b<-cbind(a,Tamil=c(56,85,49))
b
print("Summarizing the Data Frame")
summary(a)
summary(b)
print("Removing Rows/Columns")
a[-c(2),-c(3)]
b[-c(1),-c(2)]
print("Binding the Rows in Data Frame")
Name<-c("Gokila","Prathipa","Shalini")
Maths<-c(84,76,62)
Science<-c(76,78,44)
c<-[Link](Name,Maths,Science)
c
d<-rbind(c,a)
d
Output

> print("Creating Vectors")


[1] "Creating Vectors"
> Name<-c("Rekha","Divya","Gokul")
> Maths<-c(46,78,59)
> Science<-c(67,45,56)
> print("Creating Data Frames from Vectors")
[1] "Creating Data Frames from Vectors"
> a<-[Link](Name,Maths,Science)
>a
Name Maths Science
1 Rekha 46 67
2 Divya 78 45
3 Gokul 59 56
> print("To find the Dimension of the Data Frame")
[1] "To find the Dimension of the Data Frame"
> dim(a)
[1] 3 3
> print("Accessing values with Name")
[1] "Accessing values with Name"
> a["Name"]
Name
1 Rekha
2 Divya
3 Gokul
> a["Maths"]
Maths
1 46
2 78
3 59
> print("Adding a New Column")
[1] "Adding a New Column"
> b<-cbind(a,Tamil=c(56,85,49))
>b
Name Maths Science Tamil
1 Rekha 46 67 56
2 Divya 78 45 85
3 Gokul 59 56 49
> print("Summarizing the Data Frame")
[1] "Summarizing the Data Frame"
> summary(a)
Name Maths Science
Length:3 Min. :46.0 Min. :45.0
Class :character 1st Qu.:52.5 1st Qu.:50.5
Mode :character Median :59.0 Median :56.0
Mean :61.0 Mean :56.0 3rd
Qu.:68.5 3rd Qu.:61.5
Max. :78.0 Max. :67.0
> summary(b)
Name Maths Science Tamil Length:3
Min. :46.0 Min. :45.0 Min. :49.00
Class :character 1st Qu.:52.5 1st Qu.:50.5 1st Qu.:52.50 Mode
:character Median :59.0 Median :56.0 Median :56.00
Mean :61.0 Mean :56.0 Mean :63.33 3rd
Qu.:68.5 3rd Qu.:61.5 3rd Qu.:70.50 Max.
:78.0 Max. :67.0 Max. :85.00
> print("Removing Rows/Columns")
[1] "Removing Rows/Columns"
> a[-c(2),-c(3)]
Name Maths
1 Rekha 46
3 Gokul 59
> b[-c(1),-c(2)]
Name Science Tamil
2 Divya 45 85
3 Gokul 56 49
> print("Binding the Rows in Data Frame")
[1] "Binding the Rows in Data Frame"
> Name<-c("Gokila","Prathipa","Shalini")
> Maths<-c(84,76,62)
> Science<-c(76,78,44)
> c<-[Link](Name,Maths,Science)
>c
Name Maths Science
1 Gokila 84 76
2 Prathipa 76 78
3 Shalini 62 44
> d<-rbind(c,a)
>d
Name Maths Science
1 Gokila 84 76
2 Prathipa 76 78
3 Shalini 62 44
4 Rekha 46 67
5 Divya 78 45
6 Gokul 59 56
>
5. LISTS AND OPERATORS

print("Creating a List")
mylist <- list("Green","Red","Blue","Green")
mylist
print("Accesing List through Index")
mylist[2]
mylist[4]
print("Change the Item in the List")
mylist[1] <- "Orange"
mylist
print("Length of the List")
length(mylist)
print("Check whether item exists")
"Blue" %in% mylist
"Black" %in% mylist
print("Add an Item to the List")
append(mylist,"Brown")
print("Remove an Item from the List")
newlist <- mylist[-3]
newlist
print("Adding Lists")
list2 <- list("Pink","Purple","Peach")
list3 <- c(newlist,list2)
list3
Output

> print("Creating a List")


[1] "Creating a List"
> mylist <- list("Green","Red","Blue","Green")
> mylist
[[1]]
[1] "Green"

[[2]]
[1] "Red"

[[3]]
[1] "Blue"

[[4]]
[1] "Green"

> print("Accesing List through Index")


[1] "Accesing List through Index"
> mylist[2]
[[1]]
[1] "Red"

> mylist[4]
[[1]]
[1] "Green"

> print("Change the Item in the List")


[1] "Change the Item in the List"
> mylist[1] <- "Orange"
> mylist
[[1]]
[1] "Orange"

[[2]]
[1] "Red"

[[3]]
[1] "Blue"
[[4]]
[1] "Green"

> print("Length of the List")


[1] "Length of the List"
> length(mylist)
[1] 4
> print("Check whether item exists")
[1] "Check whether item exists"
> "Blue" %in% mylist
[1] TRUE
> "Black" %in% mylist
[1] FALSE
> print("Add an Item to the List")
[1] "Add an Item to the List"
> append(mylist,"Brown")
[[1]]
[1] "Orange"

[[2]]
[1] "Red"

[[3]]
[1] "Blue"

[[4]]
[1] "Green"

[[5]]
[1] "Brown"

> print("Remove an Item from the List")


[1] "Remove an Item from the List"
> newlist <- mylist[-3]
> newlist
[[1]]
[1] "Orange"

[[2]]
[1] "Red"

[[3]]
[1] "Green"

> print("Adding Lists")


[1] "Adding Lists"
> list2 <- list("Pink","Purple","Peach")
> list3 <- c(newlist,list2)
> list3
[[1]]
[1] "Orange"

[[2]]
[1] "Red"

[[3]]
[1] "Green"

[[4]]
[1] "Pink"

[[5]]
[1] "Purple"

[[6]]
[1] "Peach"
>
6. WORKING WITH LOOPING STATEMENTS

num<-[Link](readline(prompt="Enter a number:"))
print("For Loop to print Factorial")
fact<-1
for(x in 1:num)
{
fact=fact*x
}
print(paste("The Factorial of is",fact))

num<-[Link](readline(prompt="Enter a number:"))
print("While Loop to print the sum of first N numbers")
x<-1
s<-0
while(x<num)
{
s<-s+x
x<-x+1
}
print(paste("The sum of first N numbers is:",s))
Output

> num<-[Link](readline(prompt="Enter a number:"))


Enter a number:6
> print("For Loop to print Factorial")
[1] "For Loop to print Factorial"
> fact<-1
> for(x in 1:num)
+{
+ fact=fact*x
+}
> print(paste("The Factorial of is",fact))
[1] "The Factorial of is 720"
> num<-[Link](readline(prompt="Enter a number:"))
Enter a number:10
> print("While Loop to print the sum of first N numbers")
[1] "While Loop to print the sum of first N numbers"
> x<-1
> s<-0
> while(x<num)
+{
+ s<-s+x
+ x<-x+1
+}
> print(paste("The sum of first N numbers is:",s))
[1] "The sum of first N numbers is: 45"
>
7. GRAPHS IN R

print("Pie Chart")
rain <- c(22, 58, 20, 35,28)
labels <- c("Coimbatore","Ooty","Tirupur","Erode", "Salem")
piepercent<-round(100*rain/sum(rain),1)
pie(rain, labels = piepercent, col=rainbow(5),main="Rain Chart")
legend("topright", labels, cex=0.8, fill=rainbow(5))

print("Bar Chart")
m <- c("Abi", "Banu", "Deepa", "Hema", "Rani")
r <- c("CIA1", "CIA2", "Model")
v <- matrix(c(35,60,80,76,59,44,78,57,93,42,67,72,28,20,61),
nrow=3,ncol=5, byrow=TRUE)
barplot(v, main = "Mark Analysis", [Link] = m, xlab = "Student",
ylab = "Marks", col = rainbow(3), beside = TRUE)
legend("topleft", r, cex = 0.7, fill=rainbow(3))
Output
8. 3D PLOTS IN R

a)
print("Drawing a 3D Cone")
cone<-function(x,y)
{
sqrt(x^2+y^2)
}
x<-y<-seq(-1,1,length=20)
z<-outer(x,y,cone)
persp(x,y,z)
persp(x,y,z,main="3D Cone", zlab="height", theta=30, phi=15,
col="yellow", shade=0.5)

b)
[Link]("plotrix")
library(plotrix)
cust <- c(15,35,50,20,5)
labels <- c("Milkybar", "5 Star", "Dairy Milk", "Kit Kat", "Others")
pie3D(
x = cust, labels = labels, main = "3D Analysis of Chocolate Sales",
col = rainbow(5), explode = 0.1, labelcex = 0.8, theta = pi/6
)
OUTPUT

You might also like