Name :- Shrihari Shahaji Shinde Roll no :- 2470197
(F.Y.B.SC.Comp.Sci)
Date :- 28/07/2024
Title :- introduction to R
> #Q1
>
> sales=c(110,150,115,142,228,195,155,230)
> sales
[1] 110 150 115 142 228 195 155 230
>
> a=seq(1,45,4)
>a
[1] 1 5 9 13 17 21 25 29 33 37 41 45
>
> b=seq(1,126,9)
>b
[1] 1 10 19 28 37 46 55 64 73 82 91 100 109 118
>
> c=rep(5,7)
>c
[1] 5 5 5 5 5 5 5
>
> d=seq(50,150,2)
>d
[1] 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86
[20] 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124
[39] 126 128 130 132 134 136 138 140 142 144 146 148 150
>
> #Q2(1)
>
> x=c(4,5,8)
>x
[1] 4 5 8
> y=c(1,7,9,10)
>y
[1] 1 7 9 10
>
> a=c(y,x)
>a
[1] 1 7 9 10 4 5 8
>
> y1=c(y,12,6,2)
> y1
[1] 1 7 9 10 12 6 2
>
> z=c(x+y)
Warning message:
In x + y : longer object length is not a multiple of shorter object length
>z
[1] 5 12 17 14
>
> #Q2(2)
> x=c(1,2,3,9,16,18)
>x
[1] 1 2 3 9 16 18
>
> y=x^2
>y
[1] 1 4 9 81 256 324
>
> z=1/x
>z
[1] 1.00000000 0.50000000 0.33333333 0.11111111 0.06250000 0.05555556
>
> w=log10(x)
>w
[1] 0.0000000 0.3010300 0.4771213 0.9542425 1.2041200 1.2552725
>
> #Q2(3)
> s1=c(37,49,7,38)
> s1
[1] 37 49 7 38
> s2=c(16,37,21,42,27,40,39,51)
> s2
[1] 16 37 21 42 27 40 39 51
> sp=s1+s2
> sp
[1] 53 86 28 80 64 89 46 89
> sn=(s1+s2)/2
> sn
[1] 26.5 43.0 14.0 40.0 32.0 44.5 23.0 44.5
> sd=s1/s2
> sd
[1] 2.3125000 1.3243243 0.3333333 0.9047619 1.3703704 1.2250000 0.1794872
[8] 0.7450980
> sm=s1*s2
> sm
[1] 592 1813 147 1596 999 1960 273 1938
>
> #Q3(1)
> hgt=c(140,137,150,147,139,140,150,132,138,140)
> hgt
[1] 140 137 150 147 139 140 150 132 138 140
> wgt=c(55,57,59,62,61,60,60,58,59,57)
> wgt
[1] 55 57 59 62 61 60 60 58 59 57
> d=data.frame(hgt,wgt)
>d
hgt wgt
1 140 55
2 137 57
3 150 59
4 147 62
5 139 61
6 140 60
7 150 60
8 132 58
9 138 59
10 140 57
> #Q3(2)
> boys=hgt[hgt>148]
> boys
[1] 150 150
> boys=wgt[wgt>60]
> boys
[1] 62 61
> boys2=subset(d,hgt>140&wgt>60)
> boys2
hgt wgt
4 147 62
>
> #Q4)
> age=c(25,35,45,55,65,75,85,95)
> frequency=c(55,93,113,90,85,73,29,5)
> d=data.frame(age,frequency)
>d
age frequency
1 25 55
2 35 93
3 45 113
4 55 90
5 65 85
6 75 73
7 85 29
8 95 5
>
> d1=transform(d,cf=cumsum(frequency))
> d1
age frequency cf
1 25 55 55
2 35 93 148
3 45 113 261
4 55 90 351
5 65 85 436
6 75 73 509
7 85 29 538
8 95 5 543
> TF=sum(frequency)
> TF
[1] 543
>
> d2=transform(d1,rf=(frequency/TF))
> d2
age frequency cf rf
1 25 55 55 0.101289134
2 35 93 148 0.171270718
3 45 113 261 0.208103131
4 55 90 351 0.165745856
5 65 85 436 0.156537753
6 75 73 509 0.134438306
7 85 29 538 0.053406998
8 95 5 543 0.009208103
>
> d3=transform(d2,rcf=(cf/TF))
> d3
age frequency cf rf rcf
1 25 55 55 0.101289134 0.1012891
2 35 93 148 0.171270718 0.2725599
3 45 113 261 0.208103131 0.4806630
4 55 90 351 0.165745856 0.6464088
5 65 85 436 0.156537753 0.8029466
6 75 73 509 0.134438306 0.9373849
7 85 29 538 0.053406998 0.9907919
8 95 5 543 0.009208103 1.0000000
>
> #Q5)
> m=matrix(c(seq(10,60,10),5,10,120,122,13,5),nrow=6)
>m
[,1] [,2]
[1,] 10 5
[2,] 20 10
[3,] 30 120
[4,] 40 122
[5,] 50 13
[6,] 60 5
> rownames(m)=c(seq(1,6,1)
+ colnames(m)=c("Mid age","No of cases")
Error: unexpected symbol in:
"rownames(m)=c(seq(1,6,1)
colnames"
>m
[,1] [,2]
[1,] 10 5
[2,] 20 10
[3,] 30 120
[4,] 40 122
[5,] 50 13
[6,] 60 5
>
> #Q6)
> rollno=c(1,2,3,4,5,6,7)
> name=c("noq","bca","hcd","deh","ejf","flg","goh")
> hobby=c("swimming","reading","painting","football","badminton","singing","dancing")
> d=data.frame(rollno,name,hobby)
>d
rollno name hobby
1 1 noq swimming
2 2 bca reading
3 3 hcd painting
4 4 deh football
5 5 ejf badminton
6 6 flg singing
7 7 goh dancing
>
> #Q7)
> x=seq(1,30,1)
>x
[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] 26 27 28 29 30
> x1=x^2
> x1
[1] 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361
[20] 400 441 484 529 576 625 676 729 784 841 900
> x2=log10(x)
> x2
[1] 0.0000000 0.3010300 0.4771213 0.6020600 0.6989700 0.7781513 0.8450980
[8] 0.9030900 0.9542425 1.0000000 1.0413927 1.0791812 1.1139434 1.1461280
[15] 1.1760913 1.2041200 1.2304489 1.2552725 1.2787536 1.3010300 1.3222193
[22] 1.3424227 1.3617278 1.3802112 1.3979400 1.4149733 1.4313638 1.4471580
[29] 1.4623980 1.4771213
> d=data.frame(x,x1,x2)
>d
x x1 x2
1 1 1 0.0000000
2 2 4 0.3010300
3 3 9 0.4771213
4 4 16 0.6020600
5 5 25 0.6989700
6 6 36 0.7781513
7 7 49 0.8450980
8 8 64 0.9030900
9 9 81 0.9542425
10 10 100 1.0000000
11 11 121 1.0413927
12 12 144 1.0791812
13 13 169 1.1139434
14 14 196 1.1461280
15 15 225 1.1760913
16 16 256 1.2041200
17 17 289 1.2304489
18 18 324 1.2552725
19 19 361 1.2787536
20 20 400 1.3010300
21 21 441 1.3222193
22 22 484 1.3424227
23 23 529 1.3617278
24 24 576 1.3802112
25 25 625 1.3979400
26 26 676 1.4149733
27 27 729 1.4313638
28 28 784 1.4471580
29 29 841 1.4623980
30 30 900 1.4771213
>
> #Q8)
> x=c(1,5,7,2)
> y=c(3,9)
> z=x+y
>z
[1] 4 14 10 11
> w=x-y
>w
[1] -2 -4 4 -7
>
> #Q9)
> price=c(10,15,30,42,50,60)
> quantity=c(4,20,15,20,26,8)
> d=data.frame(price,quantity)
>d
price quantity
1 10 4
2 15 20
3 30 15
4 42 20
5 50 26
6 60 8
> d1=transform(d,value=(price*quantity))
> d1
price quantity value
1 10 4 40
2 15 20 300
3 30 15 450
4 42 20 840
5 50 26 1300
6 60 8 480
>
> #Q10(1)
> x=c(3,1,2,6,4,7,5,8,9,10,0)
>x
[1] 3 1 2 6 4 7 5 8 9 10 0
> #Q10(2)
> min(x)
[1] 0
> max(x)
[1] 10
> #Q10(3)
> x[x>5]
[1] 6 7 8 9 10
> x[x=0]
numeric(0)
> sort(x,decreasing=F)
[1] 0 1 2 3 4 5 6 7 8 9 10
>