0% found this document useful (0 votes)
24 views11 pages

Experiment No 8

Uploaded by

Abhijit Wabale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views11 pages

Experiment No 8

Uploaded by

Abhijit Wabale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

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

#b)Access the element numbered 3 to 6


age[3:6]

## [1] 14 16 18 20

#c)Accesssll the element expect 5th and 7th


age[-c(5,7)]

## [1] 10 12 14 16 20 19

#d)Access element numbered 2,4,6amd &


age[c(2,4,6,8)]

## [1] 12 16 20 19

#e)Argument age with adding 18 in beginning and 21,24 in the end


age1=c(18,age,21,24)
age1

## [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

#find employee with basicpay>7000


d1=subset(d,basicpay>7000)
d1

## 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)

#a)joint the variables to create data frame > df=data.frame(a,b,c,d)


df=data.frame(a,b,c,d)

#b)print the column headers


d1=colnames(df)
d1

## [1] "a" "b" "c" "d"

#c)change the column names with the function names


names(df)=c("ID", "items","store","price")
df

## ID items store price


## 1 10 Book True 2.5
## 2 20 Pen False 8.0
## 3 30 Textbook True 10.0
## 4 40 Pencil_case False 7.0

#d)print the structure


str(df)

## 'data.frame': 4 obs. of 4 variables:


## $ ID : num 10 20 30 40
## $ items: chr "Book" "Pen" "Textbook" "Pencil_case"
## $ store: chr "True" "False" "True" "False"
## $ price: num 2.5 8 10 7

#e) select the row 1 in column 2


df[1,2]

## [1] "Book"

#f) select row 1 to 2


df[1:2,]

## ID items store price


## 1 10 Book True 2.5
## 2 20 Pen False 8.0

#g)select column 1
df[1]

## ID
## 1 10
## 2 20
## 3 30
## 4 40

#h) select rows 1to 3 and column 3 to 4


df[1:3,3:4]

## 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

#j) create a new vector quantity


quantity=c(10,35,40,5)
#k) Append quantity to data frame
df$quantity=quantity
df

## ID items store price quantity


## 1 10 Book True 2.5 10
## 2 20 Pen False 8.0 35
## 3 30 Textbook True 10.0 40
## 4 40 Pencil_case False 7.0 5

#I)select column ID from data frame


df$ID

## [1] 10 20 30 40

#m)Reutrn only items with price and above 10


subset(df,subset=price>10)

## [1] ID items store price quantity


## <0 rows> (or 0-length row.names)

#n) Return only items with price and above 10


subset(df,subset=price>5)

## ID items store price quantity


## 2 20 Pen False 8 35
## 3 30 Textbook True 10 40
## 4 40 Pencil_case False 7 5

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

#a) Check type of x using typeof function


typeof(x)

## [1] "list"

#b) Check if x is a data frame or not, using class function


class(x)

## [1] "data.frame"
#c)Print names of vectors in x
names(x)

## [1] "SN" "Age" "Name"

#d)Print number of columns in x


ncol(x)

## [1] 3

#e)Print number of rows in x


nrow(x)

## [1] 2

#f)check structure of x
str(x)

## 'data.frame': 2 obs. of 3 variables:


## $ SN : int 1 2
## $ Age : num 21 15
## $ Name: Factor w/ 2 levels "Dora","John": 2 1

#g)The third colum"Name"is of type factor change this type to character


x=data.frame("SN"=1:2,"Age"=c(21,15), "Name"=c("John", "Dora"),stringsAsFacto
rs=FALSE)
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

## [,1] [,2] [,3]


## [1,] 2810 890 540
## [2,] 3542 1363 471
## [3,] 4301 1632 652
## [4,] 5362 2071 895
## [5,] 6593 2752 1113

rownames(study)=c(2015,2016,2017,2018,2019)
colnames(study)=c("Humanity","Science", "Commerce")
study

## Humanity Science Commerce


## 2015 2810 890 540
## 2016 3542 1363 471
## 2017 4301 1632 652
## 2018 5362 2071 895
## 2019 6593 2752 1113

barplot(t(study),col=c("white", "Sky blue","Blue"),main="Sub-divided barplot"


,beside=FALSE)
Que 8:
Represent data as a Multiple bar diagram
donation=matrix(c(1154,526,775,155,1700,1125,1280,560), byrow=TRUE, ncol=4);
donation

## [,1] [,2] [,3] [,4]


## [1,] 1154 526 775 155
## [2,] 1700 1125 1280 560

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")

You might also like