Metstat 1 - Malvina Vioxa - 21611145

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

Nama: Malvina Vioxa Oktamay.L.

W
NIM : 21611145
Kelas : C
Prodi : STATISTIKA
UTS TAKE HOME

SYNTAX:
#Soal Nomor 1
USD<-read.csv("/Users/Ryoto/OneDrive/Desktop/Rworkshop/data/USDat.csv")
USD
dim(USD)
USD2<read.csv("/Users/Ryoto/OneDrive/Desktop/Rworkshop/data/USDat.csv",row.n
ames =
1)
USD2
dim(USD2)
cellsdata<-ncol(USD2)*nrow(USD2)
cellsdata

#Soal Nomor 2
q2.csv<-USD2[48:50,9:11]
q2.csv

#Soal Nomor 3
q3.csv<-USD2[-1,1:2]
q3.csv

#Soal Nomor 4
q4.csv<-USD2[,-c(8,9,10,11)]
q4.csv

#Soal Nomor 5
nomor5<-USD2[,c(2:4,11)]
nomor5
dim(nomor5)
#Soal Nomor 6
criminal_type_csv<-(nomor5)
criminal_type_csv
class(criminal_type_csv)
summary(criminal_type_csv)
hist(nomor5$Assault,col = "blue")
hist(nomor5$Rape,col="green")
hist(nomor5$Income,col = "orange")
hist(nomor5$Assault,col = "blue",main = "Histogram
Assault,21611150",sub="Ujian Tengah Semester")

hist(nomor5$Income,col = "orange",main = "Histogram


Income,21611150",sub="Ujian Tengah Semester")
plot(nomor5$Assault,col="blue",main = "Plot Assault,21611150")
plot(nomor5$Rape,col="green",main = "Plot Rape,21611150")
plot(nomor5$Income,col="orange",main = "Plot Income,21611150")

#Soal Nomor 7 rownames(USD2)


[which.min(USD2$Income)] rownames(USD2)
[which.min(USD2$Literacy)] rownames(USD2)
[which.min(USD2$Life.Exp)] rownames(USD2)
[which.min(USD2$HS.Grad)]

#Soal Nomor 8
#8a
cor(USD2$Literacy,USD2$Murder)
cor.test(USD2$Literacy,USD2$Murder)
plot(USD2$Literacy~USD2$Murder)
#8b
cor(USD2$Literacy,USD2$Assault)
cor.test(USD2$Literacy,USD2$Assault)
plot(USD2$Literacy~USD2$Assault)
#8c
cor(USD2$Literacy,USD2$Rape)
cor.test(USD2$Literacy,USD2$Rape)
plot(USD2$Literacy~USD2$Rape)

#Soal Nomor 9
cor(USD2$Murder,USD2$Assault)
cor.test(USD2$Murder,USD2$Assault)
cor(USD2$Murder,USD2$Rape)
cor.test(USD2$Murder,USD2$Rape)
cor(USD2$Assault,USD2$Rape)
cor.test(USD2$Assault,USD2$Rape)
cor(USD2$Income,USD2$Literacy)
cor.test(USD2$Income,USD2$Literacy)
cor(USD2$Income,USD2$Life.Exp)
cor.test(USD2$Income,USD2$Life.Exp)
cor(USD$Income,USD2$HS.Grad)
cor.test(USD$Income,USD2$HS.Grad)
cor(USD2$Income,USD2$Density)
cor.test(USD2$Income,USD2$Density)
cor(USD2$Literacy,USD2$Life.Exp)
cor.test(USD2$Literacy,USD2$Life.Exp)
cor(USD2$Literacy,USD2$HS.Grad)
cor.test(USD2$Literacy,USD2$HS.Grad)
cor(USD2$Literacy,USD2$Density)
cor.test(USD2$Literacy,USD2$Density)
cor(USD2$Life.Exp,USD2$HS.Grad)
cor.test(USD2$Life.Exp,USD2$HS.Grad)
cor(USD2$Life.Exp,USD2$Density)
cor.test(USD2$Life.Exp,USD2$Density)
cor(USD2$HS.Grad,USD2$Density)
cor.test(USD2$HS.Grad,USD2$Density)
colnames(USD2)
colnames(USD2)[which.max(c(USD2$Income,USD2$Literacy,USD2$Life.Exp,USD2$HS.
Grad,USD2$Density,USD2$UrbanPop,USD2$Murder,USD2$Rape,USD2$Assault))]

#Soal Nomor 10
mean(USD2$Murder)
mean(USD2$Assault)
mean(USD2$Rape)
rownames(USD2)[which.min(c(USD2$Murder,USD2$Assault,USD2$Rape))]
rownames(USD2)[which.max(c(USD2$Murder,USD2$Assault,USD2$Rape))]
rownames(USD2)[which.max(USD2$Murder)] rownames(USD2)
[which.max(USD$Assault)] rownames(USD2)[which.max(USD$Rape)]
sort(c("(rownames(USD2)[which.max(USD2$Murder)])","(rownames(USD2)[which.ma
x(USD2$Assault)])","(rownames(USD2)[which.max(USD2$Rape)])"))

#Soal Nomor 11
dim(USD2$Region)
length((is.na(USD2$Region)))

#Soal Nomor 12
mean(USD2$Murder)
mean(USD2$Assault)
mean(USD2$Rape)
rownames(USD2)[which.max(c(mean(USD2$Murder),mean(USD2$Assault),mean(USD2$R
ape)))]

#Soal Nomor 13
mean(USD2$Income)
mean(USD2$Literacy)
mean(USD2$Life.Exp)
mean(USD2$HS.Grad)
mean(USD2$Density)
rownames(USD2)[which.min(c(mean(USD2$Income),mean(USD2$Literacy),mean(USD2$
Life.Exp),mean(USD2$HS.Grad),mean(USD2$Density)))]

#Soal Nomor 14 rownames(USD2)


[which.min(c(mean(USD2$Murder),mean(USD2$Assault),mean(USD2$R
ape),mean(USD2$Income),mean(USD2$Literacy),mean(USD2$Life.Exp),mean(USD2$HS
.Grad),mean(USD2$Density),mean(USD2$African.American.pct),mean(USD$UrbanPop
)))]
plot(USD2[,3], USD2[,4])
plot(USD2[,3],
USD2[,4],pch=21,bg=c("red","blue","skyblue")[unlist(USD2$Region)],xlab="X",
ylab="Y")
legend(1,2.5,pch=rep(16,3),col=c("red","blue","skyblue"),c("Murder","Assaul
t","Rape") )
legend(4,2.5, fill=c("red","blue"),c("Murder","Assault + Rape"))
hist(USD2$Murder, col = "yellow")
hist(USD2$Assault, col = "red")
hist(USD2$Rape, col = "brown")
id.Murder <- which(USD2$Murder == "Murder")
id.Assault <- which(USD2$Assault == "Assault")
id.Rape <- which(USD2$Rape == "Rape")
boxplot(USD2$Murder~USD2$Assault)
boxplot(USD2$Murder~USD2$Rape)
boxplot(USD2$Assault~USD2$Rape)
#output
> #Soal Nomor 1
> USD<-read.csv("/Users/USER/Desktop/Rworkshop/data/USDat.csv")
> USD
State Murder Assault Rape Income Literacy Life.Exp HS.Grad
Density African.American.pct UrbanPop Region
1 Alabama 13.2 236 21.2 3624 97.9 69.05 41.3
71 26.2 58 South
2 Alaska 10.0 263 44.5 6315 98.5 69.31 66.7
1 3.0 48 West
3 Arizona 8.1 294 31.0 4530 98.2 70.55 58.1
20 3.0 80 West
4 Arkansas 8.8 190 19.5 3378 98.1 70.66 39.9
41 18.3 50 South
5 California 9.0 276 40.6 5114 98.9 71.71 62.6
136 7.0 91 West
6 Colorado 7.9 204 38.7 4884 99.3 72.06 63.9
24 3.0 78 West
7 Connecticut 3.3 110 11.1 5348 98.9 72.48 56.0
638 6.0 77 Northeast
8 Delaware 5.9 238 15.8 4809 99.1 70.06 54.6
292 14.3 72 South
9 Florida 15.4 335 31.9 4815 98.7 70.66 52.6
153 15.3 80 South
10 Georgia 17.4 211 25.8 4091 98.0 68.54 40.6
85 25.9 60 South
11 Hawaii 5.3 46 20.2 4963 98.1 73.60 61.9
135 1.0 83 West
12 Idaho 2.6 120 14.2 4119 99.4 71.87 59.5
10 0.3 54 West
13 Illinois 10.4 249 24.0 5107 99.1 70.14 52.6
201 12.8 83 Midwest
14 Indiana 7.2 113 21.0 4458 99.3 70.88 52.9
147 6.9 65 Midwest
15 Iowa 2.2 56 11.3 4628 99.5 72.56 59.0
51 1.2 57 Midwest
16 Kansas 6.0 115 18.0 4669 99.4 72.58 59.9
28 4.8 66 Midwest
17 Kentucky 9.7 109 16.3 3712 98.4 70.10 38.5
85 7.2 52 South
18 Louisiana 15.4 249 22.2 3545 97.2 68.76 42.2
85 29.8 66 South
19 Maine 2.1 83 7.8 3694 99.3 70.39 54.7
34 0.3 51 Northeast
20 Maryland 11.3 300 27.8 5299 99.1 70.22 52.3
417 17.8 67 South
21 Massachusetts 4.4 149 16.3 4755 98.9 71.83 58.5
743 3.1 85 Northeast
22 Michigan 12.1 255 35.1 4751 99.1 70.63 52.8
160 11.2 74 Midwest
23 Minnesota 2.7 72 14.9 4675 99.4 72.96 57.6
49 0.9 66 Midwest
24 Mississippi 16.1 259 17.1 3098 97.6 68.09 41.0
49 36.8 44 South
25 Missouri 9.0 178 28.2 4254 99.2 70.69 48.8
69 10.3 70 Midwest
26 Montana 6.0 109 16.4 4347 99.4 70.56 59.2
5 0.3 53 West
27 Nebraska 4.3 102 16.5 4508 99.4 72.60 59.3
20 2.7 62 Midwest
28 Nevada 12.2 252 46.0 5149 99.5 69.03 65.2
5 5.7 81 West
29 New Hampshire 2.1 57 9.5 4281 99.3 71.23 57.6
90 0.3 56 Northeast
30 New Jersey 7.4 159 18.8 5237 98.9 70.93 52.5
975 10.7 89 Northeast
31 New Mexico 11.4 285 32.1 3601 97.8 70.32 55.2
9 1.9 70 West
32 New York 11.1 254 26.1 4903 98.6 70.55 52.7
378 11.9 86 Northeast
33 North Carolina 13.0 337 16.1 3875 98.2 69.21 38.5
112 22.2 45 South
34 North Dakota 0.8 45 7.3 5087 99.2 72.78 50.3
9 0.4 44 Midwest
35 Ohio 7.3 120 21.4 4561 99.2 70.82 53.2
262 9.1 75 Midwest
36 Oklahoma 6.6 151 20.0 3983 98.9 71.42 51.6
39 6.7 68 South
37 Oregon 4.9 159 29.3 4660 99.4 72.13 60.0
24 1.3 67 West
38 Pennsylvania 6.3 106 14.9 4449 99.0 70.43 50.2
264 8.6 72 Northeast
39 Rhode Island 3.4 174 8.3 4558 98.7 71.90 46.4
888 2.7 87 Northeast
40 South Carolina 14.4 279 22.5 3635 97.7 67.96 37.8
93 30.5 48 South
41 South Dakota 3.8 86 12.8 4167 99.5 72.08 53.3
9 0.2 45 Midwest
42 Tennessee 13.2 188 26.9 3821 98.3 70.11 41.8
101 15.8 59 South
43 Texas 12.7 201 25.5 4188 97.8 70.90 47.4
47 12.5 80 South
44 Utah 3.2 120 22.9 4022 99.4 72.90 67.3
15 0.6 80 West
45 Vermont 2.2 48 11.2 3907 99.4 71.64 57.1
51 0.2 32 Northeast
46 Virginia 8.5 156 20.7 4701 98.6 70.08 47.8
125 18.5 63 South
47 Washington 4.0 145 26.2 4864 99.4 71.72 63.5
53 2.1 73 West
48 West Virginia 5.7 81 9.3 3617 98.6 69.48 41.6
75 3.9 39 South
49 Wisconsin 2.6 53 10.8 4468 99.3 72.48 54.5
84 2.9 66 Midwest
50 Wyoming 6.8 161 15.6 4566 99.4 70.29 62.9
4 0.8 60 West
> dim(USD)
[1] 50 12
> USD2<-read.csv("/Users/USER/Desktop/Rworkshop/data/USDat.csv",row.names =
1)
> USD2
Murder Assault Rape Income Literacy Life.Exp HS.Grad Density
African.American.pct UrbanPop Region
Alabama 13.2 236 21.2 3624 97.9 69.05 41.3 71
26.2 58 South
Alaska 10.0 263 44.5 6315 98.5 69.31 66.7 1
3.0 48 West
Arizona 8.1 294 31.0 4530 98.2 70.55 58.1 20
3.0 80 West
Arkansas 8.8 190 19.5 3378 98.1 70.66 39.9 41
18.3 50 South
California 9.0 276 40.6 5114 98.9 71.71 62.6 136
7.0 91 West
Colorado 7.9 204 38.7 4884 99.3 72.06 63.9 24
3.0 78 West
Connecticut 3.3 110 11.1 5348 98.9 72.48 56.0 638
6.0 77 Northeast
Delaware 5.9 238 15.8 4809 99.1 70.06 54.6 292
14.3 72 South
Florida 15.4 335 31.9 4815 98.7 70.66 52.6 153
15.3 80 South
Georgia 17.4 211 25.8 4091 98.0 68.54 40.6 85
25.9 60 South
Hawaii 5.3 46 20.2 4963 98.1 73.60 61.9 135
1.0 83 West
Idaho 2.6 120 14.2 4119 99.4 71.87 59.5 10
0.3 54 West
Illinois 10.4 249 24.0 5107 99.1 70.14 52.6 201
12.8 83 Midwest
Indiana 7.2 113 21.0 4458 99.3 70.88 52.9 147
6.9 65 Midwest
Iowa 2.2 56 11.3 4628 99.5 72.56 59.0 51
1.2 57 Midwest
Kansas 6.0 115 18.0 4669 99.4 72.58 59.9 28
4.8 66 Midwest
Kentucky 9.7 109 16.3 3712 98.4 70.10 38.5 85
7.2 52 South
Louisiana 15.4 249 22.2 3545 97.2 68.76 42.2 85
29.8 66 South
Maine 2.1 83 7.8 3694 99.3 70.39 54.7 34
0.3 51 Northeast
Maryland 11.3 27.8 5299 99.1 70.22 52.3 417
300

17.8 67 South
Massachusetts 149 16.3 4755 98.9 71.83 58.5 743
4.4
3.1 85 Northeast
Michigan 12.1 35.1 4751 99.1 70.63 52.8 160
255
11.2 74 Midwest
Minnesota 2.7 72 14.9 4675 99.4 72.96 57.6 49
0.9 66 Midwest
Mississippi 16.1 259 17.1 3098 97.6 68.09 41.0 49
36.8 44 South
Missouri 9.0 178 28.2 4254 99.2 70.69 48.8 69
10.3 70 Midwest
Montana 6.0 109 16.4 4347 99.4 70.56 59.2 5
0.3 53 West
Nebraska 4.3 102 16.5 4508 99.4 72.60 59.3 20
2.7 62 Midwest
Nevada 12.2 252 46.0 5149 99.5 69.03 65.2 5
5.7 81 West
New Hampshire 2.1 57 9.5 4281 99.3 71.23 57.6 90
0.3 56 Northeast
New Jersey 7.4 159 18.8 5237 98.9 70.93 52.5 975
10.7 89 Northeast
New Mexico 11.4 285 32.1 3601 97.8 70.32 55.2 9
1.9 70 West
New York 11.1 254 26.1 4903 98.6 70.55 52.7 378
11.9 86 Northeast
North Carolina 13.0 337 16.1 3875 98.2 69.21 38.5 112
22.2 45 South
North Dakota 0.8 45 7.3 5087 99.2 72.78 50.3 9
0.4 44 Midwest
Ohio 7.3 120 21.4 4561 99.2 70.82 53.2 262
9.1 75 Midwest
Oklahoma 6.6 151 20.0 3983 98.9 71.42 51.6 39
6.7 68 South
Oregon 4.9 159 29.3 4660 99.4 72.13 60.0 24
1.3 67 West
Pennsylvania 6.3 106 14.9 4449 99.0 70.43 50.2 264
8.6 72 Northeast
Rhode Island 3.4 174 8.3 4558 98.7 71.90 46.4 888
2.7 87 Northeast
South Carolina 14.4 279 22.5 3635 97.7 67.96 37.8 93
30.5 48 South
South Dakota 3.8 86 12.8 4167 99.5 72.08 53.3 9
0.2 45 Midwest
Tennessee 13.2 188 26.9 3821 98.3 70.11 41.8 101
15.8 59 South
Texas 12.7 201 25.5 4188 97.8 70.90 47.4 47
12.5 80 South
Utah 3.2 120 22.9 4022 99.4 72.90 67.3 15
0.6 80 West
Vermont 2.2 48 11.2 3907 99.4 71.64 57.1 51
0.2 32 Northeast
Virginia 8.5 156 20.7 4701 98.6 70.08 47.8 125
18.5 63 South
Washington 4.0 145 26.2 4864 99.4 71.72 63.5 53
2.1 73 West
West Virginia 5.7 81 9.3 3617 98.6 69.48 41.6 75
3.9 39 South
Wisconsin 2.6 53 10.8 4468 99.3 72.48 54.5 84
2.9 66 Midwest
Wyoming 6.8 161 15.6 4566 99.4 70.29 62.9 4
0.8 60 West
> dim(USD2)
[1] 50 11
> cellsdata<-ncol(USD2)*nrow(USD2)
> cellsdata
[1] 550
> #Soal Nomor 2
> q2.csv<-USD2[48:50,9:11]
> q2.csv
African.American.pct UrbanPop Region
West Virginia 3.9 39 South
Wisconsin 2.9 66 Midwest
Wyoming 0.8 60 West
> #Soal Nomor 3
> q3.csv<-USD2[-1,1:2]
> q3.csv
Murder Assault
Alaska 10.0 263
Arizona 8.1 294
Arkansas 8.8 190
California 9.0 276
Colorado 7.9 204
Connecticut 3.3 110
Delaware 5.9 238
Florida 15.4 335
Georgia 17.4 211
Hawaii 5.3 46
Idaho 2.6 120
Illinois 10.4 249
Indiana 7.2 113
Iowa 2.2 56
Kansas 6.0 115
Kentucky 9.7 109
Louisiana 15.4 249
Maine 2.1 83
Maryland 11.3 300
Massachusetts 4.4 149
Michigan 12.1 255
Minnesota 2.7 72
Mississippi 16.1 259
Missouri 9.0 178
Montana 6.0 109
Nebraska 4.3 102
Nevada 12.2 252
New Hampshire 2.1 57
New Jersey 7.4 159
New Mexico 11.4 285
New York 11.1 254
North Carolina 13.0 337
North Dakota 0.8 45
Ohio 7.3 120
Oklahoma 6.6 151
Oregon 4.9 159
Pennsylvania 6.3 106
Rhode Island 3.4 174
South Carolina 14.4 279
South Dakota 3.8 86
Tennessee 13.2 188
Texas 12.7 201
Utah 3.2 120
Vermont 2.2 48
Virginia 8.5 156
Washington 4.0 145
West Virginia 5.7 81
Wisconsin 2.6 53
Wyoming 6.8 161
> #Soal Nomor 4
> q4.csv<-USD2[,-c(8,9,10,11)]
> q4.csv
Murder Assault Rape Income Literacy Life.Exp HS.Grad
Alabama 13.2 236 21.2 3624 97.9 69.05 41.3
Alaska 10.0 263 44.5 6315 98.5 69.31 66.7
Arizona 8.1 294 31.0 4530 98.2 70.55 58.1
Arkansas 8.8 190 19.5 3378 98.1 70.66 39.9
California 9.0 276 40.6 5114 98.9 71.71 62.6
Colorado 7.9 204 38.7 4884 99.3 72.06 63.9
Connecticut 3.3 110 11.1 5348 98.9 72.48 56.0
Delaware 5.9 238 15.8 4809 99.1 70.06 54.6
Florida 15.4 335 31.9 4815 98.7 70.66 52.6
Georgia 17.4 211 25.8 4091 98.0 68.54 40.6
Hawaii 5.3 46 20.2 4963 98.1 73.60 61.9
Idaho 2.6 120 14.2 4119 99.4 71.87 59.5
Illinois 10.4 249 24.0 5107 99.1 70.14 52.6
Indiana 7.2 113 21.0 4458 99.3 70.88 52.9
Iowa 2.2 56 11.3 4628 99.5 72.56 59.0
Kansas 6.0 115 18.0 4669 99.4 72.58 59.9
Kentucky 9.7 109 16.3 3712 98.4 70.10 38.5
Louisiana 15.4 249 22.2 3545 97.2 68.76 42.2
Maine 2.1 83 7.8 3694 99.3 70.39 54.7
Maryland 11.3 27.8 5299 99.1 70.22 52.3
300
Massachusetts 149 16.3 4755 98.9 71.83 58.5
4.4
Michigan 12.1 35.1 4751 99.1 70.63 52.8
255
Minnesota 2.7 72 14.9 4675 99.4 72.96 57.6
Mississippi 16.1 259 17.1 3098 97.6 68.09 41.0
Missouri 9.0 178 28.2 4254 99.2 70.69 48.8
Montana 6.0 109 16.4 4347 99.4 70.56 59.2
Nebraska 4.3 102 16.5 4508 99.4 72.60 59.3
Nevada 12.2 252 46.0 5149 99.5 69.03 65.2
New Hampshire 2.1 57 9.5 4281 99.3 71.23 57.6
New Jersey 7.4 159 18.8 5237 98.9 70.93 52.5
New Mexico 11.4 285 32.1 3601 97.8 70.32 55.2
New York 11.1 254 26.1 4903 98.6 70.55 52.7
North Carolina 13.0 337 16.1 3875 98.2 69.21 38.5
North Dakota 0.8 45 7.3 5087 99.2 72.78 50.3
Ohio 7.3 120 21.4 4561 99.2 70.82 53.2
Oklahoma 6.6 151 20.0 3983 98.9 71.42 51.6
Oregon 4.9 159 29.3 4660 99.4 72.13 60.0
Pennsylvania 6.3 106 14.9 4449 99.0 70.43 50.2
Rhode Island 3.4 174 8.3 4558 98.7 71.90 46.4
South Carolina 14.4 279 22.5 3635 97.7 67.96 37.8
South Dakota 3.8 86 12.8 4167 99.5 72.08 53.3
Tennessee 13.2 188 26.9 3821 98.3 70.11 41.8
Texas 12.7 201 25.5 4188 97.8 70.90 47.4
Utah 3.2 120 22.9 4022 99.4 72.90 67.3
Vermont 2.2 48 11.2 3907 99.4 71.64 57.1
Virginia 8.5 156 20.7 4701 98.6 70.08 47.8
Washington 4.0 145 26.2 4864 99.4 71.72 63.5
West Virginia 5.7 81 9.3 3617 98.6 69.48 41.6
Wisconsin 2.6 53 10.8 4468 99.3 72.48 54.5
Wyoming 6.8 161 15.6 4566 99.4 70.29 62.9
> #Soal Nomor 5
> nomor5<-USD2[,c(2:4,11)]
> nomor5
Assault Rape Income Region
Alabama 236 21.2 3624 South
Alaska 263 44.5 6315 West
Arizona 294 31.0 4530 West
Arkansas 190 19.5 3378 South
California 276 40.6 5114 West
Colorado 204 38.7 4884 West
Connecticut 110 11.1 5348 Northeast
Delaware 238 15.8 4809 South
Florida 335 31.9 4815 South
Georgia 211 25.8 4091 South
Hawaii 46 20.2 4963 West
Idaho 120 14.2 4119 West
Illinois 249 24.0 5107 Midwest
Indiana 113 21.0 4458 Midwest
Iowa 56 11.3 4628 Midwest
Kansas 115 18.0 4669 Midwest
Kentucky 109 16.3 3712 South
Louisiana 249 22.2 3545 South
Maine 83 7.8 3694 Northeast
Maryland 300 27.8 5299 South
Massachusetts 149 16.3 4755 Northeast
Michigan 255 35.1 4751 Midwest
Minnesota 72 14.9 4675 Midwest
Mississippi 259 17.1 3098 South
Missouri 178 28.2 4254 Midwest
Montana 109 16.4 4347 West
Nebraska 102 16.5 4508 Midwest
Nevada 252 46.0 5149 West
New Hampshire 57 9.5 4281 Northeast
New Jersey 159 18.8 5237 Northeast
New Mexico 285 32.1 3601 West
New York 254 26.1 4903 Northeast
North Carolina 337 16.1 3875 South
North Dakota 45 7.3 5087 Midwest
Ohio 120 21.4 4561 Midwest
Oklahoma 151 20.0 3983 South
Oregon 159 29.3 4660 West
Pennsylvania 106 14.9 4449 Northeast
Rhode Island 174 8.3 4558 Northeast
South Carolina 279 22.5 3635 South
South Dakota 86 12.8 4167 Midwest
Tennessee 188 26.9 3821 South
Texas 201 25.5 4188 South
Utah 120 22.9 4022 West
Vermont 48 11.2 3907 Northeast
Virginia 156 20.7 4701 South
Washington 145 26.2 4864 West
West Virginia 81 9.3 3617 South
Wisconsin 53 10.8 4468 Midwest
Wyoming 161 15.6 4566 West
> dim(nomor5)
[1] 50 4
> #Soal Nomor 6
> criminal_type_csv<-(nomor5)
> criminal_type_csv
Assault Rape Income Region
Alabama 236 21.2 3624 South
Alaska 263 44.5 6315 West
Arizona 294 31.0 4530 West
Arkansas 190 19.5 3378 South
California 276 40.6 5114 West
Colorado 204 38.7 4884 West
Connecticut 110 11.1 5348 Northeast
Delaware 238 15.8 4809 South
Florida 335 31.9 4815 South
Georgia 211 25.8 4091 South
Hawaii 46 20.2 4963 West
Idaho 120 14.2 4119 West
Illinois 249 24.0 5107 Midwest
Indiana 113 21.0 4458 Midwest
Iowa 56 11.3 4628 Midwest
Kansas 115 18.0 4669 Midwest
Kentucky 109 16.3 3712 South
Louisiana 249 22.2 3545 South
Maine 83 7.8 3694 Northeast
Maryland 300 27.8 5299 South
Massachusetts 149 16.3 4755 Northeast
Michigan 255 35.1 4751 Midwest
Minnesota 72 14.9 4675 Midwest
Mississippi 259 17.1 3098 South
Missouri 178 28.2 4254 Midwest
Montana 109 16.4 4347 West
Nebraska 102 16.5 4508 Midwest
Nevada 252 46.0 5149 West
New Hampshire 57 9.5 4281 Northeast
New Jersey 159 18.8 5237 Northeast
New Mexico 285 32.1 3601 West
New York 254 26.1 4903 Northeast
North Carolina 337 16.1 3875 South
North Dakota 45 7.3 5087 Midwest
Ohio 120 21.4 4561 Midwest
Oklahoma 151 20.0 3983 South
Oregon 159 29.3 4660 West
Pennsylvania 106 14.9 4449 Northeast
Rhode Island 174 8.3 4558 Northeast
South Carolina 279 22.5 3635 South
South Dakota 86 12.8 4167 Midwest
Tennessee 188 26.9 3821 South
Texas 201 25.5 4188 South
Utah 120 22.9 4022 West
Vermont 48 11.2 3907 Northeast
Virginia 156 20.7 4701 South
Washington 145 26.2 4864 West
West Virginia 81 9.3 3617 South
Wisconsin 53 10.8 4468 Midwest
Wyoming 161 15.6 4566 West
> class(criminal_type_csv)
[1] "data.frame"
> summary(criminal_type_csv)
Assault Rape Income Region
Min. : 45.0 Min. : 7.30 Min. :3098 Length:50
1st Qu.:109.0 1st Qu.:15.07 1st Qu.:3993 Class :character
Median :159.0 Median :20.10 Median :4519 Mode :character
Mean :170.8 Mean :21.23 Mean :4436
3rd Qu.:249.0 3rd Qu.:26.18 3rd Qu.:4814
Max. :337.0 Max. :46.00 Max. :6315
> hist(nomor5$Assault,col = "blue")
> hist(nomor5$Rape,col="green")
> hist(nomor5$Income,col = "orange")
> hist(nomor5$Assault,col = "blue",main = "Histogram
Assault,21611150",sub="Ujian Tengah Semester")
> hist(nomor5$Rape,col="green",main = "Histogram Rape,21611150",sub="Ujian
Tengah Semester")
> hist(nomor5$Income,col = "orange",main = "Histogram
Income,21611150",sub="Ujian Tengah Semester")
> plot(nomor5$Assault,col="blue",main = "Plot Assault,21611150")
> plot(nomor5$Rape,col="green",main = "Plot Rape,21611150")
> plot(nomor5$Income,col="orange",main = "Plot Income,21611150")
> #Soal Nomor 7
> rownames(USD2)[which.min(USD2$Income)]
[1] "Mississippi"
> rownames(USD2)[which.min(USD2$Literacy)]
[1] "Louisiana"
> rownames(USD2)[which.min(USD2$Life.Exp)]
[1] "South Carolina"
> rownames(USD2)[which.min(USD2$HS.Grad)]
[1] "South Carolina"
> #Soal Nomor 8
> #8a
> cor(USD2$Literacy,USD2$Murder)
[1] -0.7067756
> cor.test(USD2$Literacy,USD2$Murder)
Pearson's product-moment correlation
data: USD2$Literacy and USD2$Murder
t = -6.9217, df = 48, p-value = 9.694e-09
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.8231797 -0.5333544
sample estimates:
cor
-0.7067756
> plot(USD2$Literacy~USD2$Murder)
> #8b
> cor(USD2$Literacy,USD2$Assault)
[1] -0.511013
> cor.test(USD2$Literacy,USD2$Assault)
Pearson's product-moment correlation
data: USD2$Literacy and USD2$Assault
t = -4.1188, df = 48, p-value = 0.0001494
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.6910642 -0.2712473
sample estimates:
cor
-0.511013
> plot(USD2$Literacy~USD2$Assault)
> #8c
> cor(USD2$Literacy,USD2$Rape)
[1] -0.1545969
> cor.test(USD2$Literacy,USD2$Rape)
Pearson's product-moment correlation
data: USD2$Literacy and USD2$Rape
t = -1.0841, df = 48, p-value = 0.2837
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.4150829 0.1293155
sample estimates:
cor
-0.1545969
> plot(USD2$Literacy~USD2$Rape)
> #Soal Nomor 9
> cor(USD2$Murder,USD2$Assault)
[1] 0.8018733
> cor.test(USD2$Murder,USD2$Assault)
Pearson's product-moment correlation
data: USD2$Murder and USD2$Assault
t = 9.2981, df = 48, p-value = 2.596e-12
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.6739512 0.8831110
sample estimates:
cor
0.8018733
> cor(USD2$Murder,USD2$Rape)
[1] 0.5635788
> cor.test(USD2$Murder,USD2$Rape)
Pearson's product-moment correlation
data: USD2$Murder and USD2$Rape
t = 4.7267, df = 48, p-value = 2.031e-05
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.3383006 0.7277619
sample estimates:
cor
0.5635788
> cor(USD2$Assault,USD2$Rape)
[1] 0.6652412
> cor.test(USD2$Assault,USD2$Rape)
Pearson's product-moment correlation
data: USD2$Assault and USD2$Rape
t = 6.173, df = 48, p-value = 1.364e-07
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.4748141 0.7961645
sample estimates:
cor
0.6652412
> cor(USD2$Income,USD2$Literacy)
[1] 0.4370752
> cor.test(USD2$Income,USD2$Literacy)
Pearson's product-moment correlation
data: USD2$Income and USD2$Literacy
t = 3.3668, df = 48, p-value = 0.001505
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.1807128 0.6378257
sample estimates:
cor
0.4370752
> cor(USD2$Income,USD2$Life.Exp)
[1] 0.3402553
> cor.test(USD2$Income,USD2$Life.Exp)
Pearson's product-moment correlation
data: USD2$Income and USD2$Life.Exp
t = 2.5069, df = 48, p-value = 0.01562
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.06838424 0.56508432
sample estimates:
cor
0.3402553
> cor(USD$Income,USD2$HS.Grad)
[1] 0.6199323
> cor.test(USD$Income,USD2$HS.Grad)
Pearson's product-moment correlation
data: USD$Income and USD2$HS.Grad
t = 5.4738, df = 48, p-value = 1.579e-06
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.4128194 0.7660866
sample estimates:
cor
0.6199323
> cor(USD2$Income,USD2$Density)
[1] 0.3300371
> cor.test(USD2$Income,USD2$Density)
Pearson's product-moment correlation
data: USD2$Income and USD2$Density
t = 2.4223, df = 48, p-value = 0.01925
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.05691822 0.55719783
sample estimates:
cor
0.3300371
> cor(USD2$Literacy,USD2$Life.Exp)
[1] 0.5884779
> cor.test(USD2$Literacy,USD2$Life.Exp)
Pearson's product-moment correlation
data: USD2$Literacy and USD2$Life.Exp
t = 5.0427, df = 48, p-value = 6.969e-06
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.3708811 0.7448226
sample estimates:
cor
0.5884779
> cor(USD2$Literacy,USD2$HS.Grad)
[1] 0.6571886
> cor.test(USD2$Literacy,USD2$HS.Grad)
Pearson's product-moment correlation
data: USD2$Literacy and USD2$HS.Grad
t = 6.0408, df = 48, p-value = 2.172e-07
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.4636561 0.7908657
sample estimates:
cor
0.6571886
> cor(USD2$Literacy,USD2$Density)
[1] -0.009454494
> cor.test(USD2$Literacy,USD2$Density)
Pearson's product-moment correlation
data: USD2$Literacy and USD2$Density
t = -0.065506, df = 48, p-value = 0.948
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.2870468 0.2696027
sample estimates:
cor
-0.009454494
> cor(USD2$Life.Exp,USD2$HS.Grad)
[1] 0.5822162
> cor.test(USD2$Life.Exp,USD2$HS.Grad)
Pearson's product-moment correlation
data: USD2$Life.Exp and USD2$HS.Grad
t = 4.9613, df = 48, p-value = 9.196e-06
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.3626369 0.7405513
sample estimates:
cor
0.5822162
> cor(USD2$Life.Exp,USD2$Density)
[1] 0.09104005
> cor.test(USD2$Life.Exp,USD2$Density)
Pearson's product-moment correlation
data: USD2$Life.Exp and USD2$Density
t = 0.63337, df = 48, p-value = 0.5295
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.1921776 0.3602585
sample estimates:
cor
0.09104005
> cor(USD2$HS.Grad,USD2$Density)
[1] -0.08837479
> cor.test(USD2$HS.Grad,USD2$Density)
Pearson's product-moment correlation
data: USD2$HS.Grad and USD2$Density
t = -0.61468, df = 48, p-value = 0.5417
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.3579181 0.1947639
sample estimates:
cor
-0.08837479
> colnames(USD2)
[1] "Murder" "Assault" "Rape"
"Income" "Literacy"
[6] "Life.Exp" "HS.Grad" "Density"
"African.American.pct" "UrbanPop"
[11] "Region"
>
colnames(USD2)[which.max(c(USD2$Income,USD2$Literacy,USD2$Life.Exp,USD2$HS.
Grad,USD2$Density,USD2$UrbanPop,USD2$Murder,USD2$Rape,USD2$Assault))]
[1] "Assault"
> #Soal Nomor 10
> mean(USD2$Murder)
[1] 7.788
> mean(USD2$Assault)
[1] 170.76
> mean(USD2$Rape)
[1] 21.232
> rownames(USD2)[which.min(c(USD2$Murder,USD2$Assault,USD2$Rape))]
[1] "North Dakota"
> rownames(USD2)[which.max(c(USD2$Murder,USD2$Assault,USD2$Rape))]
[1] NA
> rownames(USD2)[which.max(USD2$Murder)]
[1] "Georgia"
> rownames(USD2)[which.max(USD$Assault)]
[1] "North Carolina"
> rownames(USD2)[which.max(USD$Rape)]
[1] "Nevada"
>
sort(c("(rownames(USD2)[which.max(USD2$Murder)])","(rownames(USD2)[which.ma
x(USD2$Assault)])","(rownames(USD2)[which.max(USD2$Rape)])"))
[1] "(rownames(USD2)[which.max(USD2$Assault)])"
"(rownames(USD2)[which.max(USD2$Murder)])"
[3] "(rownames(USD2)[which.max(USD2$Rape)])"
> #Soal Nomor 11
> dim(USD2$Region)
NULL
> length((is.na(USD2$Region)))
[1] 50
> #Soal Nomor 12
> mean(USD2$Murder)
[1] 7.788
> mean(USD2$Assault)
[1] 170.76
> mean(USD2$Rape)
[1] 21.232
>
rownames(USD2)[which.max(c(mean(USD2$Murder),mean(USD2$Assault),mean(USD2$R
ape)))]
[1] "Alaska"
> #Soal Nomor 13
> mean(USD2$Income)
[1] 4435.8
> mean(USD2$Literacy)
[1] 98.83
> mean(USD2$Life.Exp)
[1] 70.8786
> mean(USD2$HS.Grad)
[1] 53.108
> mean(USD2$Density)
[1] 149.22
>
rownames(USD2)[which.min(c(mean(USD2$Income),mean(USD2$Literacy),mean(USD2$
Life.Exp),mean(USD2$HS.Grad),mean(USD2$Density)))]
[1] "Arkansas"
> #Soal Nomor 14
>
rownames(USD2)[which.min(c(mean(USD2$Murder),mean(USD2$Assault),mean(USD2$R
ape),mean(USD2$Income),mean(USD2$Literacy),mean(USD2$Life.Exp),mean(USD2$HS
.Grad),mean(USD2$Density),mean(USD2$African.American.pct),mean(USD$UrbanPop
)))]
[1] "Alabama"
> plot(USD2[,3], USD2[,4])
> plot(USD2[,3],
USD2[,4],pch=21,bg=c("red","blue","skyblue")[unlist(USD2$Region)],xlab="X",
ylab="Y")
>
legend(1,2.5,pch=rep(16,3),col=c("red","blue","skyblue"),c("Murder","Assaul
t","Rape") )
> legend(4,2.5, fill=c("red","blue"),c("Murder","Assault + Rape"))
> hist(USD2$Murder, col = "yellow")
> hist(USD2$Assault, col = "red")
> hist(USD2$Rape, col = "brown")
> id.Murder <- which(USD2$Murder == "Murder")
> id.Assault <- which(USD2$Assault == "Assault")
> id.Rape <- which(USD2$Rape == "Rape")
> boxplot(USD2$Murder~USD2$Assault)
> boxplot(USD2$Murder~USD2$Rape)
> boxplot(USD2$Assault~USD2$Rape)

You might also like