Mittal School of Business
Lovely Professional University
Course Code: MGN619
Course Name: Business Analytics
Section: Q1303
Faculty Name:Jolly Masih
Submitted By: Samim Das
RollNo: B36
Date of Allocation: 28th August
Date of Submission:15thSeptember
1. Create 5 hypothetical vectors with 30 observations, of which at least 1 should be a
character vector
a) character1 = c('a','b','c','d','e','f','g','h','i','j','k','l','m','n'
,'o','p','q','r','s','t','u','v','w','x','y','z','ab','ac','ad','ae')
character1
Console Window:
> character1 = c('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
'p','q','r','s','t','u','v','w','x','y','z','ab','ac','ad','ae')
> character1
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o
" "p"
[17] "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "ab" "ac" "ad" "ae"
b) number1 = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,2
3,24,25,26,27,28,29,30)
number1
Console Window:
> number1 = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
25,26,27,28,29,30)
> number1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25 26 27
[28] 28 29 30
c) number2 = c(31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,5
0,51,52,53,54,55,56,57,58,59,60)
number2
Console Window:
> number2 = c(31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
52,53,54,55,56,57,58,59,60)
> number2
[1] 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
55 56 57
[28] 58 59 60
d) number3 = c(61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,8
0,81,82,83,84,85,86,87,88,89,90)
number3
Console Window:
> number3 = c(61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,
82,83,84,85,86,87,88,89,90)
> number3
[1] 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
85 86 87
[28] 88 89 90
e) number4 = c(91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107
,108,109,110,111,112,113,114,115,116,117,118,119,120)
number4
Console Window:
> number4 = c(91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,
109,110,111,112,113,114,115,116,117,118,119,120)
> number4
[1] 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
109 110
[21] 111 112 113 114 115 116 117 118 119 120
2. Create a data frame for the vectors created
Command:
data1 = data.frame (character1,number1,number2,number3,number4)
data1
Console Window:
> data1 = data.frame (character1,number1,number2,number3,number4)
> data1
character1 number1 number2 number3 number4
1 a 1 31 61 91
2 b 2 32 62 92
3 c 3 33 63 93
4 d 4 34 64 94
5 e 5 35 65 95
6 f 6 36 66 96
7 g 7 37 67 97
8 h 8 38 68 98
9 i 9 39 69 99
10 j 10 40 70 100
11 k 11 41 71 101
12 l 12 42 72 102
13 m 13 43 73 103
14 n 14 44 74 104
15 o 15 45 75 105
16 p 16 46 76 106
17 q 17 47 77 107
18 r 18 48 78 108
19 s 19 49 79 109
20 t 20 50 80 110
21 u 21 51 81 111
22 v 22 52 82 112
23 w 23 53 83 113
24 x 24 54 84 114
25 y 25 55 85 115
26 z 26 56 86 116
27 ab 27 57 87 117
28 ac 28 58 88 118
29 ad 29 59 89 119
30 ae 30 60 90 120
3. Insert a new vector in the data frame created with 30 observations.
Command:
data1$number5 = c(121,122,123,124,125,126,127,128,129,130,131,132,133,
134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150)
data1
Console Window:
> data1$number5 = c(121,122,123,124,125,126,127,128,129,130,131,132,133,134,1
35,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150)
> data1
character1 number1 number2 number3 number4 number5
1 a 1 31 61 91 121
2 b 2 32 62 92 122
3 c 3 33 63 93 123
4 d 4 34 64 94 124
5 e 5 35 65 95 125
6 f 6 36 66 96 126
7 g 7 37 67 97 127
8 h 8 38 68 98 128
9 i 9 39 69 99 129
10 j 10 40 70 100 130
11 k 11 41 71 101 131
12 l 12 42 72 102 132
13 m 13 43 73 103 133
14 n 14 44 74 104 134
15 o 15 45 75 105 135
16 p 16 46 76 106 136
17 q 17 47 77 107 137
18 r 18 48 78 108 138
19 s 19 49 79 109 139
20 t 20 50 80 110 140
21 u 21 51 81 111 141
22 v 22 52 82 112 142
23 w 23 53 83 113 143
24 x 24 54 84 114 144
25 y 25 55 85 115 145
26 z 26 56 86 116 146
27 ab 27 57 87 117 147
28 ac 28 58 88 118 148
29 ad 29 59 89 119 149
30 ae 30 60 90 120 150
4. Write a command to remove the newly created vector from the data frame.
Command:
data1$number5 = NULL
data1
Console Window:
> data1$number5 = NULL
> data1
character1 number1 number2 number3 number4
1 a 1 31 61 91
2 b 2 32 62 92
3 c 3 33 63 93
4 d 4 34 64 94
5 e 5 35 65 95
6 f 6 36 66 96
7 g 7 37 67 97
8 h 8 38 68 98
9 i 9 39 69 99
10 j 10 40 70 100
11 k 11 41 71 101
12 l 12 42 72 102
13 m 13 43 73 103
14 n 14 44 74 104
15 o 15 45 75 105
16 p 16 46 76 106
17 q 17 47 77 107
18 r 18 48 78 108
19 s 19 49 79 109
20 t 20 50 80 110
21 u 21 51 81 111
22 v 22 52 82 112
23 w 23 53 83 113
24 x 24 54 84 114
25 y 25 55 85 115
26 z 26 56 86 116
27 ab 27 57 87 117
28 ac 28 58 88 118
29 ad 29 59 89 119
30 ae 30 60 90 120
5. Write a command to swap the first and the second columns.
Command:
data1 = data1[c(1,3,2,4,5)]
data1
Console Window:
>data1 = data1[c(1,3,2,4,5)]
> data1
character1 number2 number1 number3 number4
1 a 31 1 61 91
2 b 32 2 62 92
3 c 33 3 63 93
4 d 34 4 64 94
5 e 35 5 65 95
6 f 36 6 66 96
7 g 37 7 67 97
8 h 38 8 68 98
9 i 39 9 69 99
10 j 40 10 70 100
11 k 41 11 71 101
12 l 42 12 72 102
13 m 43 13 73 103
14 n 44 14 74 104
15 o 45 15 75 105
16 p 46 16 76 106
17 q 47 17 77 107
18 r 48 18 78 108
19 s 49 19 79 109
20 t 50 20 80 110
21 u 51 21 81 111
22 v 52 22 82 112
23 w 53 23 83 113
24 x 54 24 84 114
25 y 55 25 85 115
26 z 56 26 86 116
27 ab 57 27 87 117
28 ac 58 28 88 118
29 ad 59 29 89 119
30 ae 60 30 90 120
6. Write the command to find the minimum value for all numeric vectors individually.
Command:
data1 = c(min(number1),min(number2),min(number3),min(number4))
data1
Console Window:
> data1 = c(min(number1),min(number2),min(number3),min(number4))
> data1
[1] 1 31 61 91
7. Write a command to find the maximum value of all numeric vectors individually
Command:
data1 = c(max(number1),max(number2),max(number3),max(number4))
data1
Console Window:
>data1 = c(max(number1),max(number2),max(number3),max(number4))
> data1
[1] 30 60 90 120
8. Find the sum of all the Numeric Vectors individually
Command:
data1 = c(sum(number1),sum(number2),sum(number3),sum(number4))
data1
Console Window:
> data1 = c(sum(number1),sum(number2),sum(number3),sum(number4))
> data1
[1] 465 1365 2265 3165
9. Find the Average of all Numeric vectors individually.
Command:
data1 = c(mean(number1),mean(number2),mean(number3),mean(number4))
data1
Console Window:
> data1 = c(mean(number1),mean(number2),mean(number3),mean(number4))
> data1
[1] 15.5 45.5 75.5 105.5
10. Change the row names from index form to "One", "Two", "Three", and so on.
Command:
data1 = data.frame (character1,number1,number2,number3,number4)
data1
rownames(data1) = c("one","two","three","four","five","six","seven","e
ight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","s
ixteen","seventeen","eighteen","nineteen","twenty","twenty one","twenty
two","twenty three","twenty four","twenty five","twenty six","twenty se
ven","twenty eight","twenty nine","thirty")
rownames(data1)
data1
Console Window:
> data1 = data.frame (character1,number1,number2,number3,number4)
> data1
character1 number1 number2 number3 number4
1 a 1 31 61 91
2 b 2 32 62 92
3 c 3 33 63 93
4 d 4 34 64 94
5 e 5 35 65 95
6 f 6 36 66 96
7 g 7 37 67 97
8 h 8 38 68 98
9 i 9 39 69 99
10 j 10 40 70 100
11 k 11 41 71 101
12 l 12 42 72 102
13 m 13 43 73 103
14 n 14 44 74 104
15 o 15 45 75 105
16 p 16 46 76 106
17 q 17 47 77 107
18 r 18 48 78 108
19 s 19 49 79 109
20 t 20 50 80 110
21 u 21 51 81 111
22 v 22 52 82 112
23 w 23 53 83 113
24 x 24 54 84 114
25 y 25 55 85 115
26 z 26 56 86 116
27 ab 27 57 87 117
28 ac 28 58 88 118
29 ad 29 59 89 119
30 ae 30 60 90 120
> rownames(data1) = c("one","two","three","four","five","six","seven","eight"
,"nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","s
e venteen","eighteen","nineteen","twenty","twenty one","twenty two","twenty
thr ee","twenty four","twenty five","twenty six","twenty seven","twenty
eight","t wenty nine","thirty")
> rownames(data1)
[1] "one" "two" "three" "four" "five"
[6] "six" "seven" "eight" "nine" "ten"
[11] "eleven" "twelve" "thirteen" "fourteen" "fifteen"
[16] "sixteen" "seventeen" "eighteen" "nineteen" "twenty"
[21] "twenty one" "twenty two" "twenty three" "twenty four" "twenty five
"
[26] "twenty six" "twenty seven" "twenty eight" "twenty nine" "thirty"
> data1
character1 number1 number2 number3 number4
one a 1 31 61 91
two b 2 32 62 92
three c 3 33 63 93
four d 4 34 64 94
five e 5 35 65 95
six f 6 36 66 96
seven g 7 37 67 97
eight h 8 38 68 98
nine i 9 39 69 99
ten j 10 40 70 100
eleven k 11 41 71 101
twelve l 12 42 72 102
thirteen m 13 43 73 103
fourteen n 14 44 74 104
fifteen o 15 45 75 105
sixteen p 16 46 76 106
seventeen q 17 47 77 107
eighteen r 18 48 78 108
nineteen s 19 49 79 109
twenty t 20 50 80 110
twenty one u 21 51 81 111
twenty two v 22 52 82 112
twenty three w 23 53 83 113
twenty four x 24 54 84 114
twenty five y 25 55 85 115
twenty six z 26 56 86 116
twenty seven ab 27 57 87 117
twenty eight ac 28 58 88 118
twenty nine ad 29 59 89 119
thirty ae 30 60 90 120
11. Revert back the changes in the row names to the generic index form
Command:
rownames(data1) = NULL
rownames(data1)
Console Window:
> rownames(data1)
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "1
5" "16" "17" "18" "19" "20" "21"
[22] "22" "23" "24" "25" "26" "27" "28" "29" "30"
12. Print the top six rows of the data set
Command:
head(data1, n=6)
Console Window:
> head(data1, n=6)
character1 number1 number2 number3 number4
1 a 1 31 61 91
2 b 2 32 62 92
3 c 3 33 63 93
4 d 4 34 64 94
5 e 5 35 65 95
6 f 6 36 66 96
13. Print the bottom six rows of the data set
Command:
tail(data1 ,n=6)
Console Window:
> tail(data1 ,n=6)
character1 number1 number2 number3 number4
25 y 25 55 85 115
26 z 26 56 86 116
27 ab 27 57 87 117
28 ac 28 58 88 118
29 ad 29 59 89 119
30 ae 30 60 90 120
14. Print the 7th row of the data set
Command:
data1[7,1:5]
Console Window:
> data1[7,1:5]
character1 number1 number2 number3 number4
7 g 7 37 67 97
15. Print only the second column of the data set
Command:
data1[2]
Console Window:
> data1[2]
number1
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
18 18
19 19
20 20
21 21
22 22
23 23
24 24
25 25
26 26
27 27
28 28
29 29
30 30
16. Write the command to print the number of rows and run it and print the result.
Command:
nrow(data1)
Console Window:
> nrow(data1)
[1] 30
17. Write the command to print the number of columns and run it and print the result
Command:
ncol(data1)
Console Window:
> ncol(data1)
[1] 5
18. Write a command to print the dimensions of your data set and run the command to
print the results.
Command:
dim(data1)
Console Window:
> dim(data1)
[1] 30 5
19. Write the command to print the vector names of your data set and print the result.
Command:
names(data1)
Console Window:
> names(data1)
[1] "character1" "number1" "number2" "number3" "number4"
20. Write a command to print the vector name in column 2.
Command:
names(data1[2])
Console Window:
> names(data1[2])
[1] "number1"
21. Write a command to print the row names of the data set.
Command:
row.names(data1)
Console Window:
> row.names(data1)
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "1
5" "16"
[17] "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30"
22. Write a command to print the value in the third row from the second column.
Command:
data1[3,2]
Console Window:
> data1[3,2]
[1] 3
23. Write a command to know the class of the object you are working with.
Command:
class(data1)
Console Window:
> class(data1)
[1] "data.frame"
24. Write a command to print the values in row 3, columns 2 through 5.
Command:
data1[3,c(2:5)]
Console Window:
> data1[3,c(2:5)]
number1 number2 number3 number4
3 3 33 63 93
25. Write a command to print the values in rows 3 and 5, column 2.
Command:
data1[c(3,5),2]
Console Window:
> data1[c(3,5),2]
[1] 3 5
26. Write a command to print the values in rows 3 and 5, columns 3 through 5.
Command:
data1[c(3,5),c(3:5)]
Console Window:
> data1[c(3,5),c(3:5)]
number2 number3 number4
3 33 63 93
5 35 65 95
27. Write a command to print the columns 2 through 4.
Command:
data1[,c(2:4)]
Console Window:
> data1[,c(2:4)]
number1 number2 number3
1 1 31 61
2 2 32 62
3 3 33 63
4 4 34 64
5 5 35 65
6 6 36 66
7 7 37 67
8 8 38 68
9 9 39 69
10 10 40 70
11 11 41 71
12 12 42 72
13 13 43 73
14 14 44 74
15 15 45 75
16 16 46 76
17 17 47 77
18 18 48 78
19 19 49 79
20 20 50 80
21 21 51 81
22 22 52 82
23 23 53 83
24 24 54 84
25 25 55 85
26 26 56 86
27 27 57 87
28 28 58 88
29 29 59 89
30 30 60 90
28. Write a command to print the rows 4 through 8.
Command:
data1[c(4:8),]
Console Window:
> data1[c(4:8),]
character1 number1 number2 number3 number4
4 d 4 34 64 94
5 e 5 35 65 95
6 f 6 36 66 96
7 g 7 37 67 97
8 h 8 38 68 98
30. Write a command to print the whole of row 6
Command:
data1[6,]
Console Window:
> data1[6,]
character1 number1 number2 number3 number4
6 f 6 36 66 96