R Programs
R Programs
AIM:-
R download:
https://cran.r-project.org/bin/windows/base/
RStudio download:
https://www.rstudio.com/products/rstudio/download/
http://www.r-bloggers.com/download-and-install-r-in-ubuntu/
INSTALLATION STEPS:
STEP:1
STEP:3
Click on next button and choose install location, and click on next.
STEP:4
STEP:5
RESULT:-
Exp No Experiment Name Date
1.b The Basics of R Syntax
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
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"
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
Result:
Exp No Experiment Name Date
1.d Subsetting
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
Output:
Original List:
$a
[1] 1
$b
[1] 2
$c
[1] 10
$d
[1] 20
First element of list: 1
Output:
Original list:
$a
[1] 1
$b
[1] 2
$c
[1] "Hello"
$d
[1] "GFG"
Using $ operator:
[1] "GFG"
Output:
gear hp
Ford Pantera L 5 264
Maserati Bora 5 335
Result:
Exp No Experiment Name Date
1.e Help System
help("gsub")
?gsub
help.search("chisquare")
??chisquare
Result:
Exp No Experiment Name Date
Viewing and Manipulating
2.a
Data
View(data)
Output:
View(iris)
Output:
Result:
#Manipulating data using dplyr package
install.packages("dplyr")
library("dplyr")
summary(iris)
Output:
Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 setosa :50
Median :5.800 Median :3.000 Median :4.350 Median :1.300 virginica :50
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
head(filtered,3)
Output:
tail(col1,4)
Output:
head(arranged)
Output:
head(arranged)
Output:
Result:
Exp No Experiment Name Date
2.b Plotting Data
plot(airquality$Ozone, xlab = 'ozone Concentration', ylab = 'No of Instances', main = 'Ozone levels in
NY city', col = 'green')
Output:
Output:
#Histogram Plot using hist() function
Output:
boxplot(airquality,col="Red")
Output:
Result:
Exp No Experiment Name Date
2c Reading External Data
AIM:-
print(data)
Output:
flavor number
1 pistachio 6
3 vanilla 5
4 chocolate 10
5 strawberry 2
6 neopolitan 4
head(my_data,3)
Output:
1 IND1 10 A
2 IND2 7 A
3 IND3 20 A
Result:
AIM:-
Tables in R:
data = data.frame(
table(data)
Output:
Gender
abc 0 1
cde 1 0
def 0 1
Charts in R:
Types of R – Charts
png(file = "barplot.png")
# plotting vector
dev.off()
Output:
png(file = "piechart.png")
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)
png(file = "hist.png")
dev.off()
Output:
Scatter Plot:
#defining vector
png(file = "plot.png")
# plotting
col.axis = "darkgreen")
dev.off()
Output:
Box Plot:
x <- c(42, 21, 22, 24, 25, 30, 29, 22,23, 23, 24, 28, 32, 45, 39, 40)
png(file = "boxplot.png")
# plotting
dev.off()
Output:
Result:
AIM:-
Measure of Central Tendency:
mean<-mean(iris$Petal.Length)
print(mean)
Output:
[1] 3.758
median<-median(iris$Petal.Length)
print(median)
Output:
[1] 4.35
install.packages("modeest")
library(modeest)
mode<-mfv(iris$Petal.Length)
print(mode)
Output:
Frequency Distribution:
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
print(data)
freq<-table(data)
print(freq)
hist(airquality$Temp,xlab="Temperature",ylab="count",main="frequency distribution of
Temperature",col = "white")
Output:
data
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:-
mean(iris$Petal.Length[iris$Species=="setosa"])
mean(iris$Petal.Length[iris$Species=="versicolor"])
mean(iris$Petal.Length[iris$Species=="virginica"])
Output:
[1] 1.462
[1] 4.26
[1] 5.552
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
Output:
iris$Species: setosa
[1] 0.173664
----------------------------------------------------------------------------------
iris$Species: versicolor
[1] 0.469911
----------------------------------------------------------------------------------
iris$Species: virginica
[1] 0.5518947
Output:
iris$Species: setosa
----------------------------------------------------------------------------------
iris$Species: versicolor
----------------------------------------------------------------------------------
iris$Species: virginica
#Plotting Relationship between Species and Petal.Length variables using Box and whisker plot
Output: