Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 38

R Programming

Practical
file

Index
Sno Practical Questions Page No
1 Write the R commands to do the following: a. Create a numeric, Pg: 1-5
character, integer and logical vector. b. Create two numeric vectors
x and y of different lengths. Next, observe the output of 2*x + y - 3.
c. Create a sequence of all the odd numbers between 100 and 500.
d. Calculate the mean and standard deviation of sequence created
in part c. e. Create a list containing a vector, an array and a list. f.
Create two multiplication compatible matrices M1 and M2 and
display their product. g. Display row-wise and column-wise mean
of the matrix M1. h. Combine the matrices M1 and M2 (as in
previous part) using rbind/cbind function. i.
Convert a 3x4 matrix into 2x4 matrix.
2 Use the airquality dataset and write R commands to do the Pg:4-10
following: a. Display the structure of the dataset. b. Display the
number of observations and variables in this dataset. c. Display the
count of complete rows. d. Display the data for the observation
having Temp between 70 and 80. e. Display the count of
observations where Month = 5. f. Display first 10 rows of the
dataset. g. Display summary of Wind variable. h. Display the
complete dataset sorted as per the decreasing order of Temp
variable. i. Add a new observation to this dataset
3 Download the Toy Dataset CSV file available on Kaggle via the link Pg:11-14
given ( https://www.kaggle.com/carlolepelaars/toy- dataset).
Write R commands to do the following: a. Read the CSV into a data
frame object. b. Attach the data frame to the environment. c.
Count the number of rows with City = New York City. d. Display
rows that have top 5 income values. e. Find out the number of
rows with income greater than average income. f. Find the highest
salary for the female population. g. Randomly select 10 rows from
the given dataset. h. Detach the data frame object from
the environment
4 Write R commands to do the following: a. Create a sample STUDENT Pg:15-16
table in the MySQL database. b. Insert 5 rows into the STUDENT
table. c. Display all the information stored in STUDENT table. d.
Delete all the rows from STUDENT table. e. Drop the STUDENT table

5 Using the rainfall data for the year 2015 available on the link Pg:17-21
https://data.world/rajanand/rainfall-in-india or
https://www.kaggle.com/datasets/rajanand/rainfall-in-india Write
an R script to: a. Read the first ten days of rainfall amounts from
the CSV file. b. Create a subset of the rainfall data where rain is
larger than 20. c. Find the mean rainfall for days where the rainfall
was at least 4. d. Subset the vector where rainfall is either exactly
zero, or exactly 0.6. e. Use a suitable plot to compare the rainfall
of
each day of every month, and to compare the summarized rainfall
of every month
6 Load the built-in iris dataset and write the R commands/script for Pg:22-25
the following: a. Display the summary statistics for all the
variables in the dataset. b. Plot a scatter plot for Sepal length and
Sepal width for the species “setosa”. c.
Annotate the above graph (a) with appropriate X-axis, Y-axis labels
and Main title. d. Plot a histogram for the variable Petal.length. e.
Create a boxplot for Petal width of Species=versicolor.
7 Create a tab-delimited file containing the data as shown in the Pg:26-29
table below and then read this file into a data frame. Write an R
script to do the following: a) For each year, display the country
with maximum Beer consumption and minimum Wine
consumption. b) Find the average alcohol consumption for each
country. c) Find the Beer consumption for New Zealand for the
year(s) when its spirit consumption was more than the mean spirit
consumption. d) Find the average Beer consumption/Wine
Consumption/Spirit consumption for each year. e) Plot the mean
Beer consumption/mean Wine consumption/mean Spirit
consumption for each country using a suitable plot
8 Create a package in R to perform certain basic statistics functions. Pg:30-31

9 Create a database songs that contains the fields Pg:32-34


{song_name, mood, online_link_play_song}. Create an application
where the mood of the user is given as input and the list of songs
corresponding to that mood appears as the output. The user can
listen to any song form the list via the online link given.
Practical-1
Q1. Write the R commands to do the following:

a. Create a numeric, character, integer and logical vector.


b. Create two numeric vectors x and y of different lengths. Next, observe the
output of 2*x + y - 3.

c. Create a sequence of all the odd numbers between 100 and 500.
d. Calculate the mean and standard deviation of sequence created in part c.
e. Create a list containing a vector, an array and a list.
f. Create two multiplication compatible matrices M1 and M2 and display their
product.

g. Display row-wise and column-wise mean of the matrix M1.


h. Combine the matrices M1 and M2 (as in previous part) using rbind/cbind
function.

i. Convert a 3x4 matrix into 2x4 matrix.

Code:
#Q1)
#a)
numeric.vector<- c(1,2,3,4)
character.vector<- c('a','b','c','d')
integer.vector<- c(1L,2L,3L,4L)
logical.vector<- c(TRUE,FALSE,FALSE,TRUE)

#b)
x<- c(1,2,3)
y<- c(4,5,6,7)
2*x+y-3

#c)
odd.num<- c()

1
for(i in 100:500){
if(i%%2!=0){
odd.num<-append(odd.num,i)
}
}
print(odd.num)

#d)
mean(odd.num)
sd(odd.num)

#e)
l<- list(c(1,2,3), array(1:6,dim = c(2,3,1)), list('a','b','c'))
l

#f)
m1<- matrix(c(1,4,2,5,3,6),nrow=2,ncol=3)
m2<- matrix(c(7,8,9,10,11,12),nrow=2,ncol=3)
m1%*%m2

#g)
m1

for(i in 1:nrow(m1)){
cat(paste("mean of row-",i,":"))
cat(mean(m1[i,]),"\n")
}
for(i in 1:ncol(m1)){
cat(paste("mean of col-",i,":"))

2
cat(mean(m1[,i]),"\n")
}

#h)
m1
m2
new.m1<- rbind(m1,m2)
new.m1
new.m2<- cbind(m1,m2)
new.m2

#i)
m3x4<- matrix(c(1:12),nrow=3,ncol=4)
m3x4
m2x4<- matrix(m3x4,nrow=2,ncol=4)
m2x4

Output:

3
4
5
Practical-2

Q2.Use the airquality dataset and write R commands to do the following:

a. Display the structure of the dataset.


b. Display the number of observations and variables in this dataset.
c. Display the count of complete rows.
d. Display the data for the observation having Temp between 70 and 80.
e. Display the count of observations where Month = 5.
f. Display first 10 rows of the dataset.
g. Display summary of Wind variable.
h. Display the complete dataset sorted as per the decreasing order of Temp
variable.

i. Add a new observation to this dataset.

Code:
#Q2)
#a)
str(airquality)

#b)
cat(paste("No. of observations",nrow(airquality)))

6
cat(paste("No. of variables",ncol(airquality)))

d<- data.frame(
Ozone=c(41,36,12,18,NA),
Solar=c(190,11,149,NA,NA),
Wind=c(7.4,NA,12.6,11.5,14.9)
)
d
#c)
nrow(airquality[complete.cases(airquality),])

#d)
subset(airquality,airquality$Temp>=70 & airquality$Temp<=80)

#e)
nrow(subset(airquality,airquality$Month==5))

#f)
head(airquality,10)

#g)
summary(airquality$Wind)

#h)
airquality[order(airquality$Temp,decreasing=TRUE),]

#i)
airquality<-rbind(airquality,c(121,204,9.8,98,9,29))
airquality

7
Output:

8
9
10
Practical-3

Q3.Download the Toy Dataset CSV file available on Kaggle


via the link given (
https://www.kaggle.com/carlolepelaars/toy-
dataset). Write R commands to do the following:
a. Read the CSV into a data frame object.
b. Attach the data frame to the environment.
c. Count the number of rows with City = New York City.
d. Display rows that have top 5 income values.
e. Find out the number of rows with income greater than average income.
f. Find the highest salary for the female population.
g. Randomly select 10 rows from the given dataset.
h. Detach the data frame object from the environment.

Code:
#Q3)
#a)
toy <- read.csv("toy_dataset.csv")
toy

#b)
attach(toy)

#c)
nrow(subset(toy,toy$City=='New York City'))

#d)
head(toy[order(toy$Income,decreasing=TRUE),],5)

11
#e)
nrow(subset(toy,toy$Income>=mean(toy$Income)))

#f) not working


max<-0
for(i in 1:nrow(toy)){
if(toy[i,3]=='Female'){if(toy[i,5]>max){
max<-toy[i,5]
}
}
}
cat("\n Max Salary of Female Population is :-")
max

#d<-subset(toy,toy$Gender=='Female')
#d
#head(d[order(d,decreasing=TRUE),],1)

#g)
toy[sample(nrow(toy), 5),]

#h)
detach(toy)

Output:

12
13
14
Practical-4

Q4.Write R commands to do the following:

a. Create a sample STUDENT table in the MySQL database.


b. Insert 5 rows into the STUDENT table.
c. Display all the information stored in STUDENT table.
d. Delete all the rows from STUDENT table.
e. Drop the STUDENT table.

Code:
#Q4)
#a)
library("RMySQL")
con<-
dbConnect(MySQL(),username="root",password="",host="localhost",dbname="nitika_2005
9570006")
q="CREATE TABLE STUDENT (Student_id int(5) Primary key,Student_Name varchar(30));"
dbGetQuery(con,q)

#b)
dbSendQuery(con,"insert into student(Student_id,Student_Name)values(1,'Nitika')")

#c)
dbGetQuery(con,"select * from student")

#d)
dbGetQuery(con,"delete from student")

#e)
dbSendQuery(con,'drop table student')

15
Output:

16
Practical-5

Q5.Using the rainfall data for the year 2015 available on the link
https://data.world/rajanand/rainfall-in-india or
https://www.kaggle.com/datasets/rajanand/rainfall-in-india
Write an R script to:

a. Read the first ten days of rainfall amounts from the CSV file.
b. Create a subset of the rainfall data where rain is larger than 20.
c. Find the mean rainfall for days where the rainfall was at least 4.
d. Subset the vector where rainfall is either exactly zero, or exactly 0.6.
e. Use a suitable plot to compare the rainfall of each day of every month, and
to compare the summarized rainfall of every month.

Code:
#Q5)
#a)
rainfall <- read.csv("rainfall in india 1901-2015.csv")
head(rainfall,10)

#b)-
sub<-subset(rainfall,rainfall[,(3:14)]>20)
sub

#c)-
sum<-array(1:12)
k<-array(1:12)
for(i in 1:12){sum[i]=0
k[i]=0}
for(i in 3:14){

17
for(j in 1:nrow(rainfall)){if(is.na(rainfall[j,i])!=TRUE){
if(rainfall[j,i]>4){
sum[i-2]=sum[i-2]+rainfall[j,i]
k[i-2]=k[i-2]+1
}}
}
print(sum[i-2]/k[i-2])
}

#d)-
y<-subset(x,x[,(3:14)]==0|x[,(3:14)]==0.6)
y

#e)-
z<-rainfall
for(i in 3:14){
plot(rainfall[,i],rainfall$year,xlab = "Rainfall",ylab = "YEARS",main = names(rainfall)[i])
}
month_mean<-array()
z<-subset(rainfall[,(3:14)])
#install.packages("readr")
library(readr)
for(i in 1:12){
month_mean[i]<-readr::parse_number(summary(z)[4,i])
}
barplot(month_mean,names.arg=names(z)[3:14],xlab="Months",ylab="Summarized
Values")

18
Output:

19
20
21
Practical-6

Q6.Load the built-in iris dataset and write the R commands/script for the following:

a. Display the summary statistics for all the variables in the dataset.
b. Plot a scatter plot for Sepal length and Sepal width for the species “setosa”.
c. Annotate the above graph (a) with appropriate X-axis, Y-axis labels and Main
title.

d. Plot a histogram for the variable Petal.length.


e. Create a boxplot for Petal width of Species=versicolor.

Code:
#Q6)
#a)
str(iris)

#b)
x<-(subset(iris,iris$Species=='setosa'))$Sepal.Length
y<-(subset(iris,iris$Species=='setosa'))$Sepal.Width
plot(x, y)

#c)
plot(x, y, main="Scatter plot", xlab="Sepal Length", ylab="Sepal Width", col="red", cex=1)

#d)
p<-iris$Petal.Length
hist(p,xlab="Petal Length",col="red")

22
#e)
v<-(subset(iris,iris$Species=='versicolor'))$Petal.Width
v
boxplot(v, main="Boxplot of Petal width with species versicolor", col="pink", border="red")

Output:

23
24
Practical-7
25
Q7.Create a tab-delimited file containing the data as shown in the table below and then
read this file into a data frame.
Write an R script to do the following:
Beer Wine Spirit Country Year

5.24 2.86 1.81 Australia 1998

5.15 2.87 1.77 Australia 1999

5.06 2.94 1.88 NewZealand 2002

5.07 2.95 2.07 Australia 2001

4.8 2.91 1.81 NewZealand 1999

4.97 3.01 1.86 NewZealand 2000

4.68 3.07 2.06 Australia 2004

4.58 3.13 2.12 NewZealand 2003

4.57 3.11 2.15 Australia 2006

4.49 2.59 1.77 NewZealand 1998

4.26 2.65 1.64 NewZealand 2004

a) For each year, display the country with maximum Beer consumption and
minimum Wine consumption.

b) Find the average alcohol consumption for each country.

c) Find the Beer consumption for New Zealand for the year(s) when its
spirit consumption was more than the mean spirit consumption.
d) Find the average Beer consumption/Wine Consumption/Spirit
consumption for each year.

e) Plot the mean Beer consumption/mean Wine consumption/mean Spirit


consumption for each country using a suitable plot.

Code:

#Q7)
#a)

26
d<-data.frame(read.table(file='Q7tabdeliminted.txt',header=TRUE,sep='\t'))
d
tapply(d$Beer,d$Year,max)

#b)
tapply(d$Spirit, d$Country,mean)

#c)
s1<-c(subset(d$Year,d$Spirit>mean(d$Spirit)))
s1

s2<-subset(d$Beer,d$Country=='NewZealand' & d$Year %in% s1)


s2

#d)
meanfunc<-function(x){
tapply(x,d$Year,mean)
}
meanfunc(d$Beer)
meanfunc(d$Wine)
meanfunc(d$Spirit)

#e)
mean.bear<-tapply(d$Beer, d$Country,mean)
mean.wine<-tapply(d$Beer, d$Country,mean)
mean.spirit<-tapply(d$Beer, d$Country,mean)
data<-as.matrix(data.frame(Australia=c(mean.bear[1],mean.wine[1],mean.wine[1]),
NewZealand=c(mean.bear[2],mean.wine[2],mean.wine[2])))

27
rownames(data)<-c("Beer","Wine","Spirit")
data
barplot(data,col=c("blue","red","yellow"))

Output:

28
Practical-8

Q8) Create a package in R to perform certain basic statistics functions.

Code:
#Q8)

29
install.packages(c("devtools","roxygen2"))
create("mynewPackage")
x<-"C:/Users/Bharat/Documents/R/Practical/mynewPackage/R"
setwd(x)
findmean<-function(x){
mean<-mean(x)
cat(paste("\nMean is :- ",mean))
}
findmedian<-function(x){
mid<-median(x)
cat(paste("\nMedian is :- ",mid))
}
findmode<-function(v){
uniqv <- unique(v)
mode1<-uniqv[which.max(tabulate(match(v, uniqv)))]
cat(paste("\nMode is :- ",mode1))
}

x<-seq(from=0, to=100, by=20)


x
findmean(x)
findmode(x)
findmedian(x)

Output:

30
Practical-9

Q9) Create a database songs that contains the fields {song_name, mood,

31
online_link_play_song}. Create an application where the mood of the user is given as input
and the list of songs corresponding to that mood appears as the output. The user can listen
to any song form the list via the online link given.

Code:
#Q9)
Songs<-data.frame(
Song_name=c("Anyone","Love Story","Love me like you do","Blow","Blinding
Lights","Man","Say you won't let go","Lovely","A thousand years","Senorita","Fire","Best
song ever","On","Scream","I like me better"),

Mood=c("Love","happiness","Romance","Energetic","Excited","Proud","Love","Calm","Rom
ance","Love","Energetic","Comedy","Energetic","Energetic","Love" ),
Online_link_Play_Song=c(
"https://youtu.be/KIK3azN4w34",
"https://youtu.be/8xg3vE8Ie_E",
"https://youtu.be/AJtDXIazrMo",
"https://youtu.be/qCZHarOQvc4",
"https://youtu.be/4NRXx6U8ABQ",
"https://youtu.be/AqAJLh9wuZ0",
"https://youtu.be/0yW7w8F2TVA",
"https://youtu.be/V1Pl8CzNzCw",
"https://youtu.be/rtOvBOTyX00",
"https://youtu.be/Pkh8UtuejGw",
"https://youtu.be/4ujQOR2DMFM",
"https://youtu.be/o_v9MY_FMcw",
"https://youtu.be/gwMa6gpoE9I",
"https://youtu.be/FKlGHHhTOsQ",
"https://youtu.be/BcqxLCWn-CE"))
showmood<-function(v){
for(i in 1: nrow(Songs)){
if(isTRUE(Songs$Mood[i]==v)){

32
cat(paste("\n ",Songs$Song_name[i]," ------- ",Songs$Online_link_Play_Song[i]))
}
}
}
choice<-0
x<-unique(Songs$Mood)
while(choice!=2){
cat(paste("\n1. Listen To Music\n2. Exit \n"))
choice<-as.integer(readline())
if(isTRUE(choice==1)){
cat("\nEnter Your Current Mood :- ")
m1<-readline()
showmood(m1)
}else if(isTRUE(choice==2)){
cat("\nExiting")
break;
}else{
cat("\nWrong Choice")
}
}

Output:

33
34

You might also like