Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

R programming practical file sem 4 du, Lab Reports of Advanced Computer Programming

R programming practical file containing all practicals of sem 4 du with screenshots of output

Typology: Lab Reports

2019/2020

Available from 04/25/2022

kanchan-sagar
kanchan-sagar 🇮🇳

5

(2)

10 documents

Partial preview of the text

Download R programming practical file sem 4 du and more Lab Reports Advanced Computer Programming in PDF only on Docsity! Kanchan Sagar 20/CS/21 R Programming Practical File Sem - 4 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. Solution : Part (a) Code : X <- c(1 : 10) as.numeric(x) as.character(x) Part (c) Code : for (num in 100 : 500) { if (num %% 2 == 1) { print(paste("odd number is :", num)) } } Part (d) Code : sd(my_seql) Part (e) Code : my_list <- list(c(1 : 10), array(2, 3), list) my_list Part (f) Code : a <- c(10:18) M1 <- matrix(data=a , nrow = 3, ncol = 3) M1 b <- c(1:9) M2 <- matrix(data=b , nrow = 3, ncol = 3) M2 M3 <- M1*M2 M3 Part (i) Code : B <- matrix(1 : 4, nrow = 3, ncol = 4) B B <- B[1 : 2] B 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. Solution : (a) code : aq <- airquality str(aq) (c) Code : nrow(airquality) (d) Code : subset(airquality,Temp>70 & Temp<80) o-@i@laa source Console Terminal = Jobe R R420. -/ > rbind(t, aq) ‘ozone Solar. Wind Temp 1 1 1 2 41 or 3 38 7 4 2 7 5 18 ez 6 ON 56 z 28 66 8 23 85 9 9 59 8 2 51 Na 69 7 74 16 69 a 66 as 58 18 58 as 5a 34 66 6 5” 30 58 a 62 1 53 z a 73 Month 1 9 eo =a Environment History Connections Tutorial @ tuew comaton (Coniwcion| Files Plots Packages Help Viewer aloes @ BD Heme A Name Presentation pte | Process Modeldocx 509 KB ©) public dass QuiccSert ava 18 W) doo: 15ME Wi) edocs 185K8 W) Sequence diagram of update modu... B42 KB Wi) The R ntradoce 5G W) unit? rdoce ssc6 Ke Wy unit docx BAME FS use case daigpng 359Ke BY Project Bone) = Saws Modified Feb 14 2022, 75 4am 25, 202 Feb 11, 2022, 4 Ag+ 26, 2022, 8 Apr 3, 2022, 436 Mar 8, 2022, 65 ‘Age 20, 2022, 8 Ape 21, 2022, 25 Agr 21, 2022 & Feb 2 2022, 24+ 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. Solution: (a) Code : library(readr) toy_dataset <- read_csv(“toy_dataset.csv”) (b) Code : view(toy_dataset) (c) Code : Sum(toy_dataset$City == ‘New York City’) 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. Solution : Code : #a. Create a sample STUDENT table in the MySQL database. library(RMySQL) conn<- dbConnect(MySQL(),dbname="world",user="root",host="localhost",port=3306,passwo rd="root") tables<-dbListTables(conn) #con is a predefined function tables #b. Insert 5 rows into the STUDENT table. myquery<-dbSendQuery(conn,"INSERT into student values(\"Nikita Dey\",98)") myquery<-dbSendQuery(conn,"INSERT into student values(\"Kang Tae Moo\",95)") myquery<-dbSendQuery(conn,"INSERT into student values(\"Son Jong ki\",99)") myquery<-dbSendQuery(conn,"INSERT into student values(\"Minque\",98)") myquery<-dbSendQuery(conn,"INSERT into student values(\"Eunwoo\",98)") #c.Display all the information stored in STUDENT table. #selecting rows myquery<-dbSendQuery(conn,"SELECT * from student") dbFetch(myquery,n=5) dbClearResult(myquery) #d.Delete all the rows from STUDENT table. myquery<-dbSendQuery(conn,"DELETE from student") myquery<-dbSendQuery(conn,"SELECT * from student") dbFetch(myquery,n=5) dbClearResult(myquery) #e. Drop the STUDENT table. myquery<-dbSendQuery(conn,"DROP table student") myquery<-dbSendQuery(conn,"SELECT * from student") dbDisconnect(conn) closeAllConnections() 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) Display the names of the states that received highest annual rainfall during 2015. (c) Display the average rainfall for each state (d) Using a barplot compare the quaterly rainfall for the states of J&K,GUJRAT, BENGAL, WEST MP and Tamil Nadu. (e) Display the total rainfall for each month for all the states of India.Then plot a barplot to show the rainfall trend in India. Solution : (a) Code : head(rain, 10) 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. Solution : (a) Code : summary(iris) (b) Code : Plot(iris$Sepal.Length~iris$Sepal.Width) (c) Code : Plot(iris$Sepal.Length~iris$Sepal.Width, xlab = “SepalLength”, ylab = ”SepalWidth”, main = “Iris- setosa”) (d) Code : hist(iris$Petal.Length, xlab = “petalLength”) (e) Code : boxplot(iris$Sepal.Width~iris$Species==”versicolor”) 4 1999 NewZealand 5.24 2.59 5 2000 NewZealand 5.24 2.59 6 2001 Australia 5.24 2.59 7 2002 NewZealand 5.24 2.59 8 2003 NewZealand 5.24 2.59 9 2004 Australia 5.24 2.59 10 2004 NewZealand 5.24 2.59 11 2006 Australia 5.24 2.59 b)Find the average alcohol consumption for each country. > average_country_wise <-data.frame(alcohol %>% group_by(alcohol$Country) %>% + summarise(Avgbeer=mean(alcohol$Beer,na.rm=T), + AvgWine=mean(alcohol$Wine,na.rm=T), + AvgSpirit=mean(alcohol$Spirit,na.rm=T))) > average_country_wise alcohol.Country Avgbeer AvgWine AvgSpirit 1 Australia 4.806364 2.917273 1.903636 2 NewZealand 4.806364 2.917273 1.903636 c)Find the Beer consumption for New Zealand for the year(s) > #when its spirit consumption was more than the mean spirit consumption. > new<-subset(alcohol,alcohol$Country=="NewZealand") > NZBeer<- subset(new,new$Spirit>mean(new$Spirit,na.rm=T)) > data.frame(NZBeer$Beer,NZBeer$Year,NZBeer$Country) NZBeer.Beer NZBeer.Year NZBeer.Country 1 5.06 2002 NewZealand 2 4.97 2000 NewZealand 3 4.58 2003 NewZealand d) Find the average Beer consumption/Wine Consumption/Spirit consumption for each year. > average_year_wise <-data.frame(alcohol %>% group_by(alcohol$Year) %>% + summarise(Avgbeer=mean(alcohol$Beer,na.rm=T), + AvgWine=mean(alcohol$Wine,na.rm=T), + AvgSpirit=mean(alcohol$Spirit,na.rm=T))) > average_year_wise alcohol.Year Avgbeer AvgWine AvgSpirit 1 1998 4.806364 2.917273 1.903636 2 1999 4.806364 2.917273 1.903636 3 2000 4.806364 2.917273 1.903636 4 2001 4.806364 2.917273 1.903636 5 2002 4.806364 2.917273 1.903636 6 2003 4.806364 2.917273 1.903636 7 2004 4.806364 2.917273 1.903636 8 2006 4.806364 2.917273 1.903636 e) Plot the mean Beer consumption/mean Wine consumption/mean Spirit > #consumption for each country using a suitable plot. > b<-average_country_wise$Avgbeer > w<-average_country_wise$AvgWine > s<-average_country_wise$AvgSpirit > plot(b,ylab="Average beer",xlab="Country",col="red",pch=16) > plot(w,ylab="Average wine",xlab="Country ",col="blue",pch=17) > plot(s,ylab="Average spirit",xlab="Country ",col="green",pch=15) < T OF T T T T Se Of SZ 0% au aBevany 2.0 1.8 1.6 14 1.2 1.0 ‘Country Jaaq aBesany 2.0 18 1.6 14 1.2 1.0 Country