Sei sulla pagina 1di 12

FDP IndoGlobal Group of Colleges

27th April to 1st May

R Programming Language

Assignment Submission

From : Harish Kumar Pamnani,

harish.pamnani@poornima.edu.in
Feedback of FDP
At first, want to say a big thanks to organizing committee who arrange
this FDP. This gives an immense pleasure to learner, provides a platform
to learn many other things apart from just R language. There were
number of different session which assign a new directions to participants
likewise IBM computing session by Dr. Mani Madukar.
Suggestion from my side are that
1. This would be better if you provide a feedback form to
participants where you can ask questions about sessions.
2. A Proper road map should give to participants, like wise number of
video lecture could be divide into number of days.
Assignments

Introduction to basics of R - Assignment


Find answers to log2(2^5) and log(exp(1)*exp(1)). Explain

> log2(2^5)
[1] 5
> log(exp(1)*exp(2))
[1] 3

Explore the Help tab in RStudio: Change the labels, title, etc.
> help(title)
> help("labels")

Check whether plyr package is installed on your machine. If yes, load it.
library(plyr)

Introduction to Data Frames in R - Assignment

1. Find out how to calculate median - using Help button and web search.
help(median)

median(1:4) # = 2.5 [even number]


median(c(1:3, 100, 1000)) # = 3 [odd, robust]

# Create a vector.
x <- c(12,7,3,4.2,18,2,54,-21,8,-5)

# Find Mean.
result.median <- median(x)
print(result.median)
2. Calculate mean and median of the data frame CO2.

 Mean : Average
 Median : Middle Value
 Mode : Most Often

name<-c("Mahi","Saurav","Azahar","Patudi","Sunny")

name

name<-c("Mahi","Saurav","Azahar","Patudi","Sunny","Dravid")

name

played=c(23,45,56,67,78)

played

won=c(11,23,34,45,56)

lost<-c(11,22,12,23,34)

y=c(12,34,23,45,12,15)

capatin<-data.frame(name,played,won,lost,y)

lost<-c(11,22,12,23,34,22)

capatin<-data.frame(name,played,won,lost,y)

played=c(23,45,56,67,78,33)

capatin<-data.frame(name,played,won,lost,y)

won=c(11,23,34,45,56,44)

capatin<-data.frame(name,played,won,lost,y)

view(captain)

View(captain)

View(capatin)

capatin$name
ratio=capatin$won/capatin$played
> ratio
[1] 0.4782609 0.5111111 0.6071429 0.6716418 0.7179487
[6] 1.3333333
> ratio=(capatin$won/capatin$played)*100
> ratio
[1] 47.82609 51.11111 60.71429 67.16418 71.79487
[6] 133.33333

3. Take the help of R for all the commands shown in this tutorial.

4. Import a data set from the Internet directly or through file

st=read.csv("CO3-4-5.csv")
st=st[1:12,]
View(st)

Introduction to R script - Assignment

1. Create a script and save it on Desktop as testscript.R


MyFirstScript.R
2. Load myfirstscript.R (created in this tutorial) in testscript.R and run it.

load("matrices.R")

Working Directories in RStudio – Assignment

Create a new folder on your computer and make it your working directory.
Step1 Click on files plate..left side of R Studio
Step 2 Click on Folder—Working R Dirctory
Step 3 Click on More
Step 4 Choose set as Working Directory

Indexing and Slicing Data Frames - Assignment


Create a subset from captaincy data frame with the captains who have
played > 20
matches and lost < 14 matches.
captaincy <- read.csv("CaptaincyData.csv")

# View the stored data set


View(captaincy)
data<-captaincy[captaincy$played>20 and captaincy$lost<14]
data
Creating Matrices using Data Frames - Assignment
Consider 2 vectors c(9,10,11,12) and c(13,14,15,16). Create a 4 by 2 matrix
from
these two vectors.
vect1<-c(9,10,11,12)
vect2<-c(13,14,15,16)
vect3<-c(vect1,vect2)

vect3
mat1<-matrix(vect3,nrow = 4,ncol = 3,byrow=TRUE)
mat1

Consider 2 vectors c(9,10,11,12) and c(13,14,15,16). Create a 4 by 2 matrix


from
these two vectors.
vect1<-c(9,10,11,12)
vect2<-c(13,14,15,16)
vect3<-c(vect1,vect2)

vect3
mat1<-matrix(vect3,nrow = 4,ncol = 3,byrow=TRUE)
mat1

Merging and Importing Data - Assignment

Using built-in dataset iris, implement all the functions we have learnt in this
tutorial.
library(XML)

rm(list=ls())
student=read.csv("Iris.csv")
subdata<-student[1:12,]

student1<-subdata
student1
summary(student1)
class(student1)

typeof(student1)
head(student1,2)
tail(student1,2)
str
str(student1)
student2=read.csv("Iris.csv")
View(student2)

MergeData<-merge(student1,student2,by="Student.Name")
View(MergeData)

#importating Data in Different Formats

xmldata<-xm
xmldata<-xmlToDataFrame("student2.xml")
View(xmldata)

Data types and Factors – Assignment


Using built-in dataset iris, Irs_out the categorical variables.
rm(list=ls())
st1=read.csv("Iris.csv")
View(st1)
st1<-st1[1:12,]
st1
str(st1)

2. Can you _nd a variable which is categorical, but R reads as numeric? If


yes, change
it to categorical.
st1=read.csv("Iriscsv")
View(st1)
st1<-st1[1:12,]
st1
str(st1)
print(st1$Sepal_name)
st1$Total<-factor(st1$categorical)
str(st1)
levels(st1$Total)

Lists and its Operations - Assignment


1. Create a numeric vector c(1:5) and a 5 by 3 matrix with elements from
1 to 15.
2. Create a named list with vector, matrix and iris data set.
3. Retrieve the iris data set from the list using dollar operator and
indexing method.
4. State the di_erences between the results obtained by using dollar
operator and indexing method of accessing iris.
rm(list=ls())
st=read.csv("Iris.csv")
st<-st[1:12,]
data=st[1:3,c("Sepal-width”,”petal-width)]
mat1=as.matrix(data)
mat1
myvector=c(1:5)

lst=list(st,mat1,myvector)

Plotting Histograms and Pie Chart - Assignment


1. Read the _le moviesData.csv. Create a histogram of the object named
imdb num votes
2. in this _le.Create a pie chart of the object mpaa rating.
3. Save both the plots.

rm(list=ls())
st=read.csv("moviesData.csv")
st<-st[1:12,]
View(st)
rm(list=ls())
st=read.csv("CO3-4-5.csv")
st<-st[1:12,]
hist(st$Total,main="Student Marks",
xlab = "Total MArks",xlim = c(0,18),
col="blue",breaks = 2)
generalCount<-table(st$Total)
View(generalCount)
pie(generalCount)

Plotting Bar Charts and Scatter Plot - Assignment


1. Read the _le moviesData.csv. Create a bar chart of critics score for the
_rst 10
movies.
2. Create a scatter plot of imdb rating and imdb num votes to see their
relation.
3. Save both the plots.

rm(list=ls())
st=read.csv("moviesData.csv")
st<-st[1:10,]
View(st)
barplot(st$Total,ylab = "Total",
xlab = "Total Marks",col="Blue",
ylim = c(0,10),main = "Total Marks")

plot(x=st$CO3,y=st$CO4,main = "CO3 V/S CO4",xlab = "CO3 Marks",


ylab="CO4",xlim = c(0,10),ylim = c(0,10),col="Yellow")

Introduction to ggplot2 - Assignment


1. Consider the built-in data set mtcars. Find the numerical variables in
this data set.
2. Make a scatter plot from the objects named mpg and wt in this data
set.
3. Save the plot in .jpeg format.

library("ggplot2")
rm(list = ls() )

# Declare a variable to read and store moviesData


movies <-
read.csv("C:\\Users\\Harish\\Documents\\R_Working_Directory\\moviesData.csv")
View(movies)
movies <-
read.csv("C:\\Users\\Harish\\Documents\\R_Working_Directory\\moviesData.csv")
> View(movies)
> ggplot(data=movies,mapping=aes(x=critics_score,y=audience_score))+
ggsave_save(“Answer3.jpeg”)

Aesthetic Mapping in ggplot2 - Assignment


1. Using built-in data set mtcars, draw a bar chart from the object cyl.
2. Add suitable labels to this bar chart.

movies <-
read.csv("C:\\Users\\Harish\\Documents\\R_Working_Directory\\movies
Data.csv")
View(movies)
ggplot(data=movies,mapping=aes(x=mpaa_rating))+
geom_point()
geom_bar()

Data Manipulation using dplyr Package - Assignment

1. Consider the built-in data set mtcars. Find the cars with hp greater
than 100 and cyl equal to 3.
2. Arrange the mtcars data set based on mpg variable.

library(dplyr)
rm(list = ls() )

# Declare a variable to read and store moviesData


movies <- read.csv("C:\\Users\\Harish\\Documents\\R_Working_Directory\\moviesData.csv")
View(movies)
moviesComedy<-filter(movies,genre=="Comedy")
View(moviesComedy)
MoviesImd<-arrange(movies,desc(imdb_rating))
View(MoviesImd)

More Functions in dplyr Package - Assignment

1. Use the built-in data set airquality. Using select function, select the
variables Ozone,Wind, and Temp in this data set.
2. Use the built-in data set mtcars. Rename the variables mpg and cyl
with MilesPerGallonvand Cylinder, respectively.

MoviesTGI<-select(movies,title,genre,imdb_rating)

View(MoviesTGI)

Pipe Operator - Assignment

Use the built-in data set iris. Using the pipe operator, group the owers by
their
Species.
Summarise the grouped data by the mean of Sepal.Length and Sepal.Width.

library(datasets)

data(iris)

summary(iris)

iris %>% group_by(Species) %>% summarize_all(funs(mean))

Conditional Statements - Assignment

1. Use the built-in data set iris. Find the Species, in which Sepal.Length is
greater than Petal.Length.
2. Count all such Species.

# Print sepal measurements for setosa (by name)


iris[iris$Species == "setosa", c("Sepal.Length", "Sepal.Width")]

# Print sepal measurements for setosa (by index)


iris[iris$Species == "setosa", 1:2]
dim(iris)

Functions in R - Assignment
1. Create a function which computes combination of two numbers.
2. Create a function which takes a natural number as an argument, and
prints Fibonacci series. For example, consider fibonacci(5). It should
print the _rst 5 elements of Fibonacci series, i.e. 1, 1, 2, 3, 5.

combinations(3,2,letters[1:3])
combinations(3,2,letters[1:3],repeats=TRUE)

recurse_fibonacci <- function(n) {


if(n <= 1) {
return(n)
} else {
return(recurse_fibonacci(n-1) + recurse_fibonacci(n-2))
}
}
# take input from the user
nterms = as.integer(readline(prompt="How many terms? "))
# check if the number of terms is valid
if(nterms <= 0) {
print("Plese enter a positive integer")
} else {
print("Fibonacci sequence:")
for(i in 0:(nterms-1)) {
print(recurse_fibonacci(i))
}
}

Potrebbero piacerti anche