Experiment No 8
Experiment No 8
Que 1:
Create a Vector
age=c(10,12,14,16,18,20,22,19)
a=age[age>15]
a
## [1] 16 18 20 22 19
b=age[age<20]
b
## [1] 10 12 14 16 18 19
c=intersect(a,b)
c
## [1] 16 18 19
d=length(c)
d
## [1] 3
## [1] 14 16 18 20
## [1] 10 12 14 16 20 19
## [1] 12 16 20 19
## [1] 18 10 12 14 16 18 20 22 19 21 24
Que 2:
Create a data frame.
x=c("Soham", "Rohan", "Mohan", "Arnav","Aryan")
y=c(12500,9000,7500,5600,3200)
d=data.frame("Employee_name"=x,"basicpay"=y)
d
## Employee_name basicpay
## 1 Soham 12500
## 2 Rohan 9000
## 3 Mohan 7500
## 4 Arnav 5600
## 5 Aryan 3200
## Employee_name basicpay
## 1 Soham 12500
## 2 Rohan 9000
## 3 Mohan 7500
Que 3:
Create a,b,c,d variables
a=c(10,20,30,40)
b=c("Book", "Pen", "Textbook", "Pencil_case")
c=c("True", "False","True", "False")
d=c(2.5,8,10,7)
## [1] "Book"
#g)select column 1
df[1]
## ID
## 1 10
## 2 20
## 3 30
## 4 40
## store price
## 1 True 2.5
## 2 False 8.0
## 3 True 10.0
#i) slice with column name(extract the first two columns using their names)
df[,c("ID", "items")]
## ID items
## 1 10 Book
## 2 20 Pen
## 3 30 Textbook
## 4 40 Pencil_case
## [1] 10 20 30 40
Que 4:
Create data frame
x=data.frame("SN"=1:2,"Age"=c(21,15), "Name"=c("John", "Dora"),stringsAsFacto
rs=TRUE)
x
## SN Age Name
## 1 1 21 John
## 2 2 15 Dora
## [1] "list"
## [1] "data.frame"
#c)Print names of vectors in x
names(x)
## [1] 3
## [1] 2
#f)check structure of x
str(x)
## SN Age Name
## 1 1 21 John
## 2 2 15 Dora
typeof(x$Name)
## [1] "character"
Que 5:
Represent data by bar chart
Year=c(2014,2015,2016,2017,2018,2019)
Annual_sales=c(15.0,25.0,27.0,28.0,26.0,26.6)
barplot(Annual_sales,names.arg=Year,xlab="Year",ylab="Sales",col="Blue")
Que 6:
Barplot of frequencies and proportion.
x=c(3,4,1,1,3,4,3,3,1,3,2,1,2,1,2,3,2,3,1,1,1,1,4,3,1)
t=table(x)
t
## x
## 1 2 3 4
## 10 4 8 3
#i)Barplot of frequencies
barplot(t,xlab="Soft-drink",ylab="Frequency of Preferences", main="Barplot",c
ol="Sky blue")
#ii) Barplot of proportion
barplot(t/length(x),xlab="Soft-drink",ylab="Frequency of Preferences",main="B
arplot",col="yellow")
Que 7:
Represent data as a subdivided barplot
study=matrix(c(2810,890,540,3542,1363,471,4301,1632,652,5362,2071,895,6593,27
52,1113),byrow=TRUE,ncol=3)
study
rownames(study)=c(2015,2016,2017,2018,2019)
colnames(study)=c("Humanity","Science", "Commerce")
study
rownames(donation)=c("2014","2019")
colnames(donation)=c("O","A", "B","AB")
barplot(t(donation), col=c("white", "Sky blue", "Blue", "Black"), main="Multi
ple barplot", beside=TRUE)
Que 9:
Represent the data by pie chart
tax=c(6526,7108,2568,560,763)
names(tax)=c("Excise","Custom","Corporation tax","Income tax", "Other")
pie(tax,main="The tax revenue of INDIA",col=c("red", "yellow", "blue", "orang
e","green"))
Que 10:
Draw stem and leaf diagram
hotels=c(20,14,21,29,43,17,15,26,8,14,39,23,16,46,28,11,26,35,26,28,30,22,23,
7,32,19,22,18,27,9)
hotels
## [1] 20 14 21 29 43 17 15 26 8 14 39 23 16 46 28 11 26 35 26 28 30 22 23
7 32
## [26] 19 22 18 27 9
stem(hotels)
##
## The decimal point is 1 digit(s) to the right of the |
##
## 0 | 789
## 1 | 144
## 1 | 56789
## 2 | 012233
## 2 | 6667889
## 3 | 02
## 3 | 59
## 4 | 3
## 4 | 6
Que 11:
Histogram of the data.
xi=c(12.5,37.5,62.5,87.5,112.5)
fi=c(5,8,13,11,3)
y=rep(xi,fi)
brk=seq(0,125,25)
hist(y,breaks=brk,xlab="Sales", main="Histogram",col="Brown")