This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
# Print the mtcars data set\
mtcars
# Use the question mark to get information about the data set
?mtcars
Data_Cars <- mtcars
# Use dim() to find the dimension of the data set
dim(Data_Cars)
[1] 32 11
# Use names() to find the names of the variables from the data set
names(Data_Cars)
[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am"
[10] "gear" "carb"
# Menampilkan fungsi rownames() untuk mendapatkan nama tiap baris di kolom pertama, yang merupakan nama tiap mobil :
Data_Cars <- mtcars
rownames(Data_Cars)
[1] "Mazda RX4" "Mazda RX4 Wag"
[3] "Datsun 710" "Hornet 4 Drive"
[5] "Hornet Sportabout" "Valiant"
[7] "Duster 360" "Merc 240D"
[9] "Merc 230" "Merc 280"
[11] "Merc 280C" "Merc 450SE"
[13] "Merc 450SL" "Merc 450SLC"
[15] "Cadillac Fleetwood" "Lincoln Continental"
[17] "Chrysler Imperial" "Fiat 128"
[19] "Honda Civic" "Toyota Corolla"
[21] "Toyota Corona" "Dodge Challenger"
[23] "AMC Javelin" "Camaro Z28"
[25] "Pontiac Firebird" "Fiat X1-9"
[27] "Porsche 914-2" "Lotus Europa"
[29] "Ford Pantera L" "Ferrari Dino"
[31] "Maserati Bora" "Volvo 142E"
# Jika Kita ingin mencetak semua nilai yang dimiliki variabel, akses bingkai data dengan menggunakan tanda $, dan nama variabel (misalnya cyl (silinder)) :
Data_Cars <- mtcars
Data_Cars$cyl
[1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
# Fungsi sort() untuk mensorting data
Data_Cars <- mtcars
sort(Data_Cars$cyl)
[1] 4 4 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 8 8 8 8 8 8 8 8 8 8 8 8 8 8
# Kita dapat menggunakan fungsi summary() untuk mendapatkan ringkasan statistik dari data
Data_Cars <- mtcars
summary(Data_Cars)
mpg cyl disp hp
Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
Median :19.20 Median :6.000 Median :196.3 Median :123.0
Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
drat wt qsec vs
Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
Median :3.695 Median :3.325 Median :17.71 Median :0.0000
Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
am gear carb
Min. :0.0000 Min. :3.000 Min. :1.000
1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
Median :0.0000 Median :4.000 Median :2.000
Mean :0.4062 Mean :3.688 Mean :2.812
3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
Max. :1.0000 Max. :5.000 Max. :8.000
# Menemukan nilai terbesar dan terkecil dari variabel hp (hourse power) :
Data_Cars <- mtcars
max(Data_Cars$hp)
[1] 335
min(Data_Cars$hp)
[1] 52
# Kita dapat menggunakan fungsi which.max() dan which.min() untuk menemukan posisi index dari nilai max dan min dalam table :
Data_Cars <- mtcars
which.max(Data_Cars$hp)
[1] 31
which.min(Data_Cars$hp)
[1] 19
# Kita dapat menggabungkan fungsi which.max() dan which.min() dengan fungsi rownames() untuk mendapatkan nama mobil dengan tenaga kuda terbesar dan terkecil :
Data_Cars <- mtcars
rownames(Data_Cars)[which.max(Data_Cars$hp)]
[1] "Maserati Bora"
rownames(Data_Cars)[which.min(Data_Cars$hp)]
[1] "Honda Civic"
# Menemukan rata-rata dari berat (wt) sebuah mobil :
Data_Cars <- mtcars
mean(Data_Cars$wt)
[1] 3.21725
# Menemukan nilai tengah dari berat (wt) sebuah mobil:
Data_Cars <- mtcars
median(Data_Cars$wt)
[1] 3.325
# Menemukan nilai mode dari berat (wt) sebuah mobil :
Data_Cars <- mtcars
names(sort(-table(Data_Cars$wt)))[1]
[1] "3.44"
# Menemukan Berapa 75.persentil dari berat mobil? Jawabannya adalah 3,61 atau 3.610 lbs artinya 75% atau berat mobil 3.610 lbs atau kurang :
Data_Cars <- mtcars
# c() specifies which percentile you want
quantile(Data_Cars$wt, c(0.75))
75%
3.61
# Jika menjalankan fungsi quantile() tanpa menentukan parameter c(), akan didapatkan persentil 0, 25, 50, 75, dan 100 :
Data_Cars <- mtcars
quantile(Data_Cars$wt)
0% 25% 50% 75% 100%
1.51300 2.58125 3.32500 3.61000 5.42400
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.