You are on page 1of 9

1.

Create 5 hypothetical vectors with 28 observations, of which at least 2 should be a character


vector (one of the vector name should be first name of the student).

Ans.
name=c("mohit","sahil","suman","rahul","ridhi","simran","abhijeet","raghav","zoya","Vinay","ro
han","Mohan","rohit","Esha","Sajali","samriti","Mukesh","Ganesh","Tarun","rajan","rameet","Se
ema","trigoon","Pandit","komal","anmol","pream","akhil")

observation=c(5,3,1,2,4,6,7,8,9,10,44,59,61,76,57,54,17,18,19,20,37,45,42,40,38,35,31,30)

age=c(33,17,27,34,9,10,12,13,14,15,16,38,40,20,41,21,45,23,25,44,29,30,31,49,52,55,58,57)

days=c(40,41,81,80,79,69,64,63,62,61,22,21,23,25,26,27,54,49,31,44,36,35,38,46,55,60,74,77)

money=c(31,35,36,37,38,39,40,42,43,48,49,47,44,46,55,57,59,58,51,62,63,25,24,28,26,22,23,25)

bikes=c("pulsar","apache","hero","honda","isuzi","tvs","hulk","himalayas","bullet","thunderbolt"
,"kawasaki","harley","vently","platina","starcity","avenger","suzuki","splender","discover","yam
aha","ktm","duke","fazer","chetak","activa","duet","mastero","kinatic")

output
> name=c("mohit","sahil","suman","rahul","ridhi","simran","abhijeet","raghav"
,"zoya","Vinay","rohan","Mohan","rohit","Esha","Sajali","samriti","Mukesh","G
anesh","Tarun","rajan","rameet","Seema","trigoon","Pandit","komal","anmol","p
ream","akhil")
> observation=c(5,3,1,2,4,6,7,8,9,10,44,59,61,76,57,54,17,18,19,20,37,45,42,4
0,38,35,31,30)
> age=c(33,17,27,34,9,10,12,13,14,15,16,38,40,20,41,21,45,23,25,44,29,30,31,4
9,52,55,58,57)
> days=c(40,41,81,80,79,69,64,63,62,61,22,21,23,25,26,27,54,49,31,44,36,35,38
,46,55,60,74,77)
> money=c(31,35,36,37,38,39,40,42,43,48,49,47,44,46,55,57,59,58,51,62,63,25,2
4,28,26,22,23,25)
> bikes=c("pulsar","apache","hero","honda","isuzi","tvs","hulk","himalayas","
bullet","thunderbolt","kawasaki","harley","vently","platina","starcity","aven
ger","suzuki","splender","discover","yamaha","ktm","duke","fazer","chetak","a
ctiva","duet","mastero","kinatic")

2. Create a data frame for the vectors created (Name of the data frame should be surname of
the student).
Ans.
Surname=data.frame(name,observation,age,days,money,bikes)
Surname

Output
> Surname=data.frame(name,observation,age,days,money,bikes)
> Surname
name observation age days money bikes
1 mohit 5 33 40 31 pulsar
2 sahil 3 17 41 35 apache
3 suman 1 27 81 36 hero
4 rahul 2 34 80 37 honda
5 ridhi 4 9 79 38 isuzi
6 simran 6 10 69 39 tvs
7 abhijeet 7 12 64 40 hulk
8 raghav 8 13 63 42 himalayas
9 zoya 9 14 62 43 bullet
10 Vinay 10 15 61 48 thunderbolt
11 rohan 44 16 22 49 kawasaki
12 Mohan 59 38 21 47 harley
13 rohit 61 40 23 44 vently
14 Esha 76 20 25 46 platina
15 Sajali 57 41 26 55 starcity
16 samriti 54 21 27 57 avenger
17 Mukesh 17 45 54 59 suzuki
18 Ganesh 18 23 49 58 splender
19 Tarun 19 25 31 51 discover
20 rajan 20 44 44 62 yamaha
21 rameet 37 29 36 63 ktm
22 Seema 45 30 35 25 duke
23 trigoon 42 31 38 24 fazer
24 Pandit 40 49 46 28 chetak
25 komal 38 52 55 26 activa
26 anmol 35 55 60 22 duet
27 pream 31 58 74 23 mastero
28 akhil 30 57 77 25 kinatic

3 Write a command to display the top and bottom 6 observation in the data frame you created.

Ans.

head(Surname,n=6)

tail(Surname)

Output
> head(Surname,n=6)
name observation age days money bikes
1 mohit 5 33 40 31 pulsar
2 sahil 3 17 41 35 apache
3 suman 1 27 81 36 hero
4 rahul 2 34 80 37 honda
5 ridhi 4 9 79 38 isuzi
6 simran 6 10 69 39 tvs
> tail(Surname)
name observation age days money bikes
23 trigoon 42 31 38 24 fazer
24 Pandit 40 49 46 28 chetak
25 komal 38 52 55 26 activa
26 anmol 35 55 60 22 duet
27 pream 31 58 74 23 mastero
28 akhil 30 57 77 25 kinatic
>

4 Create and insert a new vector in the data frame created.

Ans.

size= c(1,2,5,3,6,4,7,9,8,10,12,11,14,15,16,13,17,18,19,20,25,24,26,21,23,22,27,29)

Surname= data.frame(name,bikes,observation,age,money,days,size)

Surname

Output
> size= c(1,2,5,3,6,4,7,9,8,10,12,11,14,15,16,13,17,18,19,20,25,24,26,21,23,2
2,27,29)
> Surname= data.frame(name,bikes,observation,age,money,days,size)
> Surname
name bikes observation age money days size
1 mohit pulsar 5 33 31 40 1
2 sahil apache 3 17 35 41 2
3 suman hero 1 27 36 81 5
4 rahul honda 2 34 37 80 3
5 ridhi isuzi 4 9 38 79 6
6 simran tvs 6 10 39 69 4
7 abhijeet hulk 7 12 40 64 7
8 raghav himalayas 8 13 42 63 9
9 zoya bullet 9 14 43 62 8
10 Vinay thunderbolt 10 15 48 61 10
11 rohan kawasaki 44 16 49 22 12
12 Mohan harley 59 38 47 21 11
13 rohit vently 61 40 44 23 14
14 Esha platina 76 20 46 25 15
15 Sajali starcity 57 41 55 26 16
16 samriti avenger 54 21 57 27 13
17 Mukesh suzuki 17 45 59 54 17
18 Ganesh splender 18 23 58 49 18
19 Tarun discover 19 25 51 31 19
20 rajan yamaha 20 44 62 44 20
21 rameet ktm 37 29 63 36 25
22 Seema duke 45 30 25 35 24
23 trigoon fazer 42 31 24 38 26
24 Pandit chetak 40 49 28 46 21
25 komal activa 38 52 26 55 23
26 anmol duet 35 55 22 60 22
27 pream mastero 31 58 23 74 27
28 akhil kinatic 30 57 25 77 29
5 Remove the newly created vector from the data frame.

Ans

Surname$size=NULL

Surname

Output
> Surname$size=NULL
> Surname
name bikes observation age money days
1 mohit pulsar 5 33 31 40
2 sahil apache 3 17 35 41
3 suman hero 1 27 36 81
4 rahul honda 2 34 37 80
5 ridhi isuzi 4 9 38 79
6 simran tvs 6 10 39 69
7 abhijeet hulk 7 12 40 64
8 raghav himalayas 8 13 42 63
9 zoya bullet 9 14 43 62
10 Vinay thunderbolt 10 15 48 61
11 rohan kawasaki 44 16 49 22
12 Mohan harley 59 38 47 21
13 rohit vently 61 40 44 23
14 Esha platina 76 20 46 25
15 Sajali starcity 57 41 55 26
16 samriti avenger 54 21 57 27
17 Mukesh suzuki 17 45 59 54
18 Ganesh splender 18 23 58 49
19 Tarun discover 19 25 51 31
20 rajan yamaha 20 44 62 44
21 rameet ktm 37 29 63 36
22 Seema duke 45 30 25 35
23 trigoon fazer 42 31 24 38
24 Pandit chetak 40 49 28 46
25 komal activa 38 52 26 55
26 anmol duet 35 55 22 60
27 pream mastero 31 58 23 74
28 akhil kinatic 30 57 25 77

6 Display the values of second column and all rows.

Ans

Surname[c(1:28),2]

Output
> Surname[c(1:28),2]
[1] pulsar apache hero honda isuzi tvs
[7] hulk himalayas bullet thunderbolt kawasaki harley
[13] vently platina starcity avenger suzuki splender
[19] discover yamaha ktm duke fazer chetak
[25] activa duet mastero kinatic
28 Levels: activa apache avenger bullet chetak discover duet duke fazer ... y
amaha

7 Print the summary of the statistics.

Ans

summary(Surname)

Output
> summary(Surname)
name bikes observation age money
abhijeet: 1 activa : 1 Min. : 1.00 Min. : 9.00 Min. :22.00
akhil : 1 apache : 1 1st Qu.: 7.75 1st Qu.:16.75 1st Qu.:30.25
anmol : 1 avenger : 1 Median :25.00 Median :29.50 Median :41.00
Esha : 1 bullet : 1 Mean :27.79 Mean :30.64 Mean :41.18
Ganesh : 1 chetak : 1 3rd Qu.:42.50 3rd Qu.:41.75 3rd Qu.:49.50
komal : 1 discover: 1 Max. :76.00 Max. :58.00 Max. :63.00
(Other) :22 (Other) :22
days
Min. :21.00
1st Qu.:34.00
Median :47.50
Mean :49.39
3rd Qu.:63.25
Max. :81.00

Q8. Create the subset using the data except 7th and 9th row and 2nd to 3rd column the data set.

Ans

Surname[c(-7,-9),c(2:3)]

Output
> Surname[c(-7,-9),c(2:3)]
bikes observation
1 pulsar 5
2 apache 3
3 hero 1
4 honda 2
5 isuzi 4
6 tvs 6
8 himalayas 8
10 thunderbolt 10
11 kawasaki 44
12 harley 59
13 vently 61
14 platina 76
15 starcity 57
16 avenger 54
17 suzuki 17
18 splender 18
19 discover 19
20 yamaha 20
21 ktm 37
22 duke 45
23 fazer 42
24 chetak 40
25 activa 38
26 duet 35
27 mastero 31
28 kinatic 30

9 Create the subset using the values in row 3, columns 2 through 5.

Ans

y=Surname[3,2:5]

Output
> y
bikes observation age money
3 hero 1 27 36

10 Write a command to know the class of the object you are working with.

Ans

class(Surname)

Output
> class(Surname)
[1] "data.frame"

11 Write a command to find the location of the file.

Ans

getwd()

Output
> getwd()
[1] "C:/Users/User/Documents"

Q12. Create a CSV file containing the two vectors with 5 observations and import this file in R.

Ans

name=c("sunny","sahil","suman","mohit","anup")

state=c("punjab","himachal","assam","gujrat","tamilnadu")

aa=matrix(c(name,state),nrow=10,ncol=5)

write.table(aa,"C:/Users/User/Desktop/R.csv",sep=",")

exel=read.csv(file.choose(),sep=",")

exel

Output
> name=c("sunny","sahil","suman","mohit","anup")
> state=c("punjab","himachal","assam","gujrat","tamilnadu")
> aa=matrix(c(name,state),nrow=10,ncol=5)
> write.table(aa,"C:/Users/User/Desktop/R.csv",sep=",")
> exel=read.csv(file.choose(),sep=",")
> exel
V1 V2 V3 V4 V5
1 sunny sunny sunny sunny sunny
2 sahil sahil sahil sahil sahil
3 suman suman suman suman suman
4 mohit mohit mohit mohit mohit
5 anup anup anup anup anup
6 punjab punjab punjab punjab punjab
7 himachal himachal himachal himachal himachal
8 assam assam assam assam assam
9 gujrat gujrat gujrat gujrat gujrat
10 tamilnadu tamilnadu tamilnadu tamilnadu tamilnadu

Q13. Create a new vector doing a sum of two vectors created in question 1.

Ans

name=c("sunny","sahil","suman","mohit","anup")

state=c("punjab","himachal","assam","gujrat","tamilnadu")

aa=matrix(c(name,state),nrow=10,ncol=5)

aa
Output
> name=c("sunny","sahil","suman","mohit","anup")
> state=c("punjab","himachal","assam","gujrat","tamilnadu")
> aa=matrix(c(name,state),nrow=10,ncol=5)
> aa
[,1] [,2] [,3] [,4] [,5]
[1,] "sunny" "sunny" "sunny" "sunny" "sunny"
[2,] "sahil" "sahil" "sahil" "sahil" "sahil"
[3,] "suman" "suman" "suman" "suman" "suman"
[4,] "mohit" "mohit" "mohit" "mohit" "mohit"
[5,] "anup" "anup" "anup" "anup" "anup"
[6,] "punjab" "punjab" "punjab" "punjab" "punjab"
[7,] "himachal" "himachal" "himachal" "himachal" "himachal"
[8,] "assam" "assam" "assam" "assam" "assam"
[9,] "gujrat" "gujrat" "gujrat" "gujrat" "gujrat"
[10,] "tamilnadu" "tamilnadu" "tamilnadu" "tamilnadu" "tamilnadu"

>

Q14. Create a character object with your name and then convert it into numeric data using nested
commands.

Ans

Sahil=c("sachin","sameer","sona")

Sharma=as.numeric(as.factor(Sahil))

Sharma

Output
> Sahil=c("sachin","sameer","sona")
> Sharma=as.numeric(as.factor(Sahil))
> Sharma
[1] 1 2 3

Q15. Create a matrix of 5X5 with hypothetical values and where row name should be first character of
your name and columns name should be first character of surname. (For Ex. If name is Kriti Bedi then
Row names should be K1, K2, K3, K4,K5 and columns name should be B1, B2, B3,B4,B5)

Ans

c1=c(13,15,25,36,45)

c2=c("shubham","anup","rohtash","rahul","rohit")

c3=c(111,222,333,444,555)

c4=c("a","b","c","d","e")

c5=c(30,40,50,60,70)
cb7=data.frame(c1,c2,c3,c4,c5)

row.names(cb7)=c("c1","c2","c3","c4","c5")

colnames(cb7)=c("c1","c2","c3","c4","c5")

cb7

Output
> c1=c(13,15,25,36,45)
> c3=c(111,222,333,444,555)
> c4=c("a","b","c","d","e")
> c2=c("shubham","anup","rohtash","rahul","rohit")
> c5=c(30,40,50,60,70)
> cb7=data.frame(c1,c2,c3,c4,c5)
> colnames(cb7)=c("c1","c2","c3","c4","c5")
> row.names(cb7)=c("c1","c2","c3","c4","c5")
> cb7
c1 c2 c3 c4 c5
c1 13 shubham 111 a 30
c2 15 anup 222 b 40
c3 25 rohtash 333 c 50
c4 36 rahul 444 d 60
c5 45 rohit 555 e 70

You might also like