You are on page 1of 30

Exp No Experiment Name Date

1.a INSTALLATION OF ‘R’

AIM:-

How to get R and R-Studio

To download and work with R on Windows operating system:

R download:

https://cran.r-project.org/bin/windows/base/

To download and work with RStudio

RStudio download:

For Windows operating system:

https://www.rstudio.com/products/rstudio/download/

For Linux operating system:

http://www.r-bloggers.com/download-and-install-r-in-ubuntu/

INSTALLATION STEPS:

STEP:1

Download R-studio 1.1.383-windows vista/7/8/10 and R-console.


STEP:2

Open R-studio setup wizard

STEP:3

Click on next button and choose install location, and click on next.
STEP:4

Install wizard will appear and click on Install button.

STEP:5

Click on FINISH button , Hence R-studio installed successfully in your P.C.

RESULT:-
Exp No Experiment Name Date
1.b The Basics of R Syntax

#Assigning values to variables


a=10
b<-5
4->c
cat(a,b,c)

Output:
10 5 4

#Arithmetic Operations
add=2 + 2 #addition
sub=2-2 #subtraction
mul=2*2 #multiplication
div=2 / 11 #returns arithmetic Quotient
rem=10 %% 2 #returns remainder
div2=2%/%11 #returns integer Quotient
cat("Addition=",add,"Subtraction=",sub,"Multiplication=",mul,"\nDevision=",div,"Remainder=",rem,
"Integer Quotient=",div2)

Output:
Addition= 4 Subtraction= 0 Multiplication= 4
Devision= 0.1818182 Remainder= 0 Integer Quotient= 0

#Relational Operators
a <- c(1, 3, 5)
b <- c(2, 4, 6)
print(a>b)
print(a<b)
print(a<=b)
print(a>=b)
print(a==b)
print(a>=b)

Output:
[1] FALSE FALSE FALSE
[1] TRUE TRUE TRUE
[1] TRUE TRUE TRUE
[1] FALSE FALSE FALSE
[1] FALSE FALSE FALSE
[1] FALSE FALSE FALSE

#Logical Operators
a <- c(3, 0, TRUE, 2+2i)
b <- c(2, 4, TRUE, 2+3i)
print(a&b)
print(a|b)
print(!a)
print(a&&b)
print(a||b)
Output:
[1] TRUE FALSE TRUE TRUE
[1] TRUE TRUE TRUE TRUE
[1] FALSE TRUE FALSE FALSE
[1] TRUE
[1] TRUE

Result:
Exp No Experiment Name Date
1.c Matrices and Lists

Matrices in R:
#Matrix with one column
data=c(1, 2, 3, 4, 5, 6)
amatrix = matrix(data)
print(amatrix)

Output:
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6

#Matrix with two columns


amatrix = matrix(data, ncol=2)
print(amatrix)

Output:
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6

#Matrix multiplication
a=matrix(c(1,2,3,4),ncol=2)
print(a)
b=matrix(c(2,2,2,2),ncol=2)
print(b)
print(a%*%b)

Output:
[,1] [,2]
[1,] 1 3
[2,] 2 4
[,1] [,2]
[1,] 2 2
[2,] 2 2
[,1] [,2]
[1,] 8 8
[2,] 12 12

Lists in R:
#list with same datatype
list1<-list(1,2,3)
list2<-list("CSE","ECE","EEE")
print(list1)
print(list2)
Output:
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[1]]
[1] "CSE"

[[2]]
[1] "ECE"

[[3]]
[1] "EEE"

#list with different datatypes


listdata<-list("CSE","ECE",c(1,2,3,4,5),TRUE,FALSE,22.5,12L)
print(listdata)

Output:
[[1]]
[1] "CSE"

[[2]]
[1] "ECE"

[[3]]
[1] 1 2 3 4 5

[[4]]
[1] TRUE

[[5]]
[1] FALSE

[[6]]
[1] 22.5

[[7]]
[1] 12

#list with different objects and names for list values


listdata<-list("CSE","ECE",c(1,2,3,4,5),TRUE,FALSE,22.5,12L)
print(listdata)
listdata <- list(c("UDay","Kumar","Venkatesh"), matrix(c(40,80,60,70,90,80), nrow = 2),
list("CSE","ECE","EEE"))
names(listdata) <- c("Students", "Marks", "Course")
print(listdata)
Output:
$Students
[1] "UDay" "Kumar" "Venkatesh"
$Marks
[,1] [,2] [,3]
[1,] 40 60 90
[2,] 80 70 80
$Course
$Course[[1]]
[1] "CSE"
$Course[[2]]
[1] "ECE"
$Course[[3]]
[1] "EEE"

Result:
Exp No Experiment Name Date
1.d Subsetting

#Subsetting Using [ ] Operator


x <- 1:15
cat("Original vector: ", x, "\n")
cat("First 5 values of vector: ", x[1:5])
cat("Without values present at index 1, 2 and 3: ",
x[-c(1, 2, 3)], "\n")

Output:
Original vector: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
First 5 values of vector: 1 2 3 4 5
Without values present at index 1, 2 and 3: 4 5 6 7 8 9 10 11 12 13 14 15

#subsetting using [[ ]] Operator


ls <- list(a = 1, b = 2, c = 10, d = 20)
cat("Original List: \n")
print(ls)
cat("First element of list: ", ls[[1]], "\n")

Output:
Original List:
$a
[1] 1
$b
[1] 2
$c
[1] 10
$d
[1] 20
First element of list: 1

#Subsetting using $ Operator


ls <- list(a = 1, b = 2, c = "Hello", d = "GFG")
cat("Original list:\n")
print(ls)
cat("Using $ operator:\n")
print(ls$d)

Output:
Original list:
$a
[1] 1
$b
[1] 2
$c
[1] "Hello"
$d
[1] "GFG"
Using $ operator:
[1] "GFG"

#Subsetting using subset( ) function


mtc <- subset(mtcars, gear == 5 & hp > 200,select = c(gear, hp))
print(mtc)

Output:
gear hp
Ford Pantera L 5 264
Maserati Bora 5 335

Result:
Exp No Experiment Name Date
1.e Help System

#getting help manual


help.start()

#getting help on a particular function

help("gsub")

#or ? operator is used instead of help function

?gsub

#help.search() or ?? is used when name of function is unaware

help.search("chisquare")

??chisquare

Result:
Exp No Experiment Name Date
Viewing and Manipulating
2.a
Data

#Viewing data in a dataframe

data <- data.frame(x=1:50,y=100:149)

View(data)

Output:

#viewing iris data from the built in iris dataset

View(iris)

Output:

Result:
#Manipulating data using dplyr package

#installing dplyr package

install.packages("dplyr")

#loading dplyr package

library("dplyr")

#summary of iris dataset using summary() function

summary(iris)

Output:

Sepal.Length Sepal.Width Petal.Length Petal.Width Species

Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 setosa :50

1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300 versicolor:50

Median :5.800 Median :3.000 Median :4.350 Median :1.300 virginica :50

Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199

3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800

Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500

#selecting data using select() function

selected <- select(iris, Sepal.Length, Sepal.Width)

head(selected)

Output:

Sepal.Length Sepal.Width

1 5.1 3.5

2 4.9 3.0

3 4.7 3.2

4 4.6 3.1

5 5.0 3.6

6 5.4 3.9
#Filtering data using filter() function

filtered <- filter(iris, Species == "setosa" )

head(filtered,3)

Output:

Sepal.Length Sepal.Width Petal.Length Petal.Width Species

1 5.1 3.5 1.4 0.2 setosa

2 4.9 3.0 1.4 0.2 setosa

3 4.7 3.2 1.3 0.2 setosa

#creating a “Greater.Half” column based on condition using Mutate() function

col1 <- mutate(iris, Greater.Half = Sepal.Width > 0.5 * Sepal.Length)

tail(col1,4)

Output:

Sepal.Length Sepal.Width Petal.Length Petal.Width Species


Greater.Half

147 6.3 2.5 5.0 1.9 virginica FALSE

148 6.5 3.0 5.2 2.0 virginica FALSE

149 6.2 3.4 5.4 2.3 virginica TRUE

150 5.9 3.0 5.1 1.8 virginica TRUE

#Arranging data in an order using arrange() function

arranged <- arrange(iris, Sepal.Width)

head(arranged)

Output:

Sepal.Length Sepal.Width Petal.Length Petal.Width Species


1 5.0 2.0 3.5 1.0 versicolor

2 6.0 2.2 4.0 1.0 versicolor

3 6.2 2.2 4.5 1.5 versicolor

4 6.0 2.2 5.0 1.5 virginica

5 4.5 2.3 1.3 0.3 setosa

6 5.5 2.3 4.0 1.3 versicolor

#To arrange Sepal Width in descending order

arranged <- arrange(iris, desc(Sepal.Width))

head(arranged)

Output:

Sepal.Length Sepal.Width Petal.Length Petal.Width Species

1 5.7 4.4 1.5 0.4 setosa

2 5.5 4.2 1.4 0.2 setosa

3 5.2 4.1 1.5 0.1 setosa

4 5.8 4.0 1.2 0.2 setosa

5 5.4 3.9 1.7 0.4 setosa

6 5.4 3.9 1.3 0.4 setosa

Result:
Exp No Experiment Name Date
2.b Plotting Data

#Plotting graphfor Ozone levels in airquality dataset using plot() function

plot(airquality$Ozone, xlab = 'ozone Concentration', ylab = 'No of Instances', main = 'Ozone levels in
NY city', col = 'green')

Output:

# Horizontal bar plot

barplot(airquality$Ozone, main = 'Ozone Concenteration in air',xlab = 'ozone levels', col=


'green',horiz = TRUE)

Output:
#Histogram Plot using hist() function

hist(airquality$Solar.R, main = 'Solar Radiation values in air',xlab = 'Solar rad.', col='red')

Output:

#Box plot using boxplot() function

boxplot(airquality,col="Red")

Output:

Result:
Exp No Experiment Name Date
2c Reading External Data

AIM:-

#reading data using read.csv() function

data=read.csv("U:/KEC/R Lab Experiments/sample.txt",header=TRUE,sep=",",dec=".")

print(data)

Output:

flavor number

1 pistachio 6

2 mint chocolate chip 7

3 vanilla 5

4 chocolate 10

5 strawberry 2

6 neopolitan 4

#reading a file from internet

my_data <- read.csv("http://www.sthda.com/upload/boxplot_format.txt",sep="\t")

head(my_data,3)

Output:

Nom variable Group

1 IND1 10 A

2 IND2 7 A

3 IND3 20 A
Result:

Exp No Experiment Name Date


3a Tables, Charts And Plots

AIM:-

Tables in R:

#creating table using table() function

data = data.frame(

"Name" = c("abc", "cde", "def"),

"Gender" = c("Male", "Female", "Male")

table(data)

Output:

Gender

Name Female Male

abc 0 1

cde 1 0

def 0 1

Charts in R:

Types of R – Charts

• Bar Plot or Bar Chart


• Pie Diagram or Pie Chart
• Histogram
• Scatter Plot
• Box Plot

Bar Plot or Bar Chart:


# defining vector

x <- c(7, 15, 23, 12, 44, 56, 32)

# output to be present as PNG file

png(file = "barplot.png")

# plotting vector

barplot(x, xlab = "sales", ylab = "Count", col = "white", col.axis = "darkgreen",

col.lab = "darkgreen",main="sales of product")

dev.off()

Output:

Pie Diagram or Pie Chart

# defining vector x with number of articles

x <- c(210, 450, 250, 100, 50, 90)

# defining labels for each value in x

names(x) <- c("Algo", "DS", "Java", "C", "C++", "Python")

# output to be present as PNG file

png(file = "piechart.png")

# creating pie chart


pie(x, labels = names(x), col = "white", main = "programming languages",

radius = -1, col.main = "red")

# saving the file

dev.off()

Output:

Histogram Plot:

# defining vector

x <- c(21, 23, 56, 90, 20, 7, 94, 12, 57, 76, 69, 45, 34, 32, 49, 55, 57)

# output to be present as PNG file

png(file = "hist.png")

hist(x, main = "Histogram of Vector x",xlab = "Values",col.lab = "darkgreen",col.main = darkgreen")

# saving the file

dev.off()

Output:
Scatter Plot:

#defining vector

orange <- Orange[, c('age', 'circumference')]

# output to be present as PNG file

png(file = "plot.png")

# plotting

plot(x = orange$age, y = orange$circumference, xlab = "Age",

ylab = "Circumference", main = "Age VS Circumference",

col.lab = "darkgreen", col.main = "darkgreen",

col.axis = "darkgreen")

# saving the file

dev.off()

Output:
Box Plot:

# defining vector with ages of employees

x <- c(42, 21, 22, 24, 25, 30, 29, 22,23, 23, 24, 28, 32, 45, 39, 40)

# output to be present as PNG file

png(file = "boxplot.png")

# plotting

boxplot(x, xlab = "Box Plot", ylab = "Age",col.axis = "darkgreen", col.lab = "darkgreen")

# saving the file

dev.off()

Output:
Result:

Exp No Experiment Name Date


Univariate Data, Measure of Central
3b
Tendency, Frequency Distribution

AIM:-
Measure of Central Tendency:

#finding mean/average of iris$Petal.Length

mean<-mean(iris$Petal.Length)

print(mean)

Output:

[1] 3.758

#finding mode or middle value of iris$Petal.Length

median<-median(iris$Petal.Length)

print(median)

Output:

[1] 4.35

#finding mode value of iris$Petal.Length

#installing and loading modest library

install.packages("modeest")

library(modeest)

mode<-mfv(iris$Petal.Length)

print(mode)

Output:

[1] 1.4 1.5

Frequency Distribution:

Frequency distribution for Categorical Data:

#finding frequency distribution of carburators in mtcars dataset using table() function

freq<-table(mtcars$carb)

print(freq)
barplot(freq,xlab="carburators",ylab="count",main="frequency distribution of carburators",col =
"black")

Output:

1 2 3 4 6 8

7 10 3 10 1 1

Frequency distribution for Continues Data:

data<-cut(airquality$Temp,9) #cut() is used to make equally spaced bins

print(data)

freq<-table(data)

print(freq)

hist(airquality$Temp,xlab="Temperature",ylab="count",main="frequency distribution of
Temperature",col = "white")

Output:

data

(56,60.6] (60.6,65.1] (65.1,69.7] (69.7,74.2] (74.2,78.8] (78.8,83.3] (83.3,87.9]

8 10 14 16 26 35 22

(87.9,92.4] (92.4,97]

15 7
Result:
Exp No Experiment Name Date
Multivariate data, Relationship
3C between categorical and
continues variables

AIM:-

Finding Relationship between Species and Petal.Length variables in iris dataset

#mean of Petal.Length for setosa Species

mean(iris$Petal.Length[iris$Species=="setosa"])

#mean of Petal.Length for setosa Species

mean(iris$Petal.Length[iris$Species=="versicolor"])

#mean of Petal.Length for setosa Species

mean(iris$Petal.Length[iris$Species=="virginica"])

Output:

[1] 1.462

[1] 4.26

[1] 5.552

#finding mean of Petal.Length for all Species using by() function

by(iris$Petal.Length, iris$Species, mean)

Output:

iris$Species: setosa

[1] 1.462

----------------------------------------------------------------------------------

iris$Species: versicolor

[1] 4.26

----------------------------------------------------------------------------------
iris$Species: virginica

[1] 5.552

#finding Standard Deviation of Petal.Length for all Species using by() function

by(iris$Petal.Length, iris$Species, sd)

Output:

iris$Species: setosa

[1] 0.173664

----------------------------------------------------------------------------------

iris$Species: versicolor

[1] 0.469911

----------------------------------------------------------------------------------

iris$Species: virginica

[1] 0.5518947

#finding summary of Petal.Length for all Species using by() function

by(iris$Petal.Length, iris$Species, summary)

Output:

iris$Species: setosa

Min. 1st Qu. Median Mean 3rd Qu. Max.

1.000 1.400 1.500 1.462 1.575 1.900

----------------------------------------------------------------------------------

iris$Species: versicolor

Min. 1st Qu. Median Mean 3rd Qu. Max.

3.00 4.00 4.35 4.26 4.60 5.10

----------------------------------------------------------------------------------
iris$Species: virginica

Min. 1st Qu. Median Mean 3rd Qu. Max.

4.500 5.100 5.550 5.552 5.875 6.900

#Plotting Relationship between Species and Petal.Length variables using Box and whisker plot

boxplot(Petal.Length~Species,data=iris, xlab="Species",ylab="Petal Length",main="Box and whisker


plot for Species and Petal Length",col=c("red","green","blue"))

Output:

You might also like