library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)  
## Warning: package 'tidyr' was built under R version 4.5.2
library(ggplot2)  
## Warning: package 'ggplot2' was built under R version 4.5.2
# NO 10
data1 <- datasets::ChickWeight
data1
##     weight Time Chick Diet
## 1       42    0     1    1
## 2       51    2     1    1
## 3       59    4     1    1
## 4       64    6     1    1
## 5       76    8     1    1
## 6       93   10     1    1
## 7      106   12     1    1
## 8      125   14     1    1
## 9      149   16     1    1
## 10     171   18     1    1
## 11     199   20     1    1
## 12     205   21     1    1
## 13      40    0     2    1
## 14      49    2     2    1
## 15      58    4     2    1
## 16      72    6     2    1
## 17      84    8     2    1
## 18     103   10     2    1
## 19     122   12     2    1
## 20     138   14     2    1
## 21     162   16     2    1
## 22     187   18     2    1
## 23     209   20     2    1
## 24     215   21     2    1
## 25      43    0     3    1
## 26      39    2     3    1
## 27      55    4     3    1
## 28      67    6     3    1
## 29      84    8     3    1
## 30      99   10     3    1
## 31     115   12     3    1
## 32     138   14     3    1
## 33     163   16     3    1
## 34     187   18     3    1
## 35     198   20     3    1
## 36     202   21     3    1
## 37      42    0     4    1
## 38      49    2     4    1
## 39      56    4     4    1
## 40      67    6     4    1
## 41      74    8     4    1
## 42      87   10     4    1
## 43     102   12     4    1
## 44     108   14     4    1
## 45     136   16     4    1
## 46     154   18     4    1
## 47     160   20     4    1
## 48     157   21     4    1
## 49      41    0     5    1
## 50      42    2     5    1
## 51      48    4     5    1
## 52      60    6     5    1
## 53      79    8     5    1
## 54     106   10     5    1
## 55     141   12     5    1
## 56     164   14     5    1
## 57     197   16     5    1
## 58     199   18     5    1
## 59     220   20     5    1
## 60     223   21     5    1
## 61      41    0     6    1
## 62      49    2     6    1
## 63      59    4     6    1
## 64      74    6     6    1
## 65      97    8     6    1
## 66     124   10     6    1
## 67     141   12     6    1
## 68     148   14     6    1
## 69     155   16     6    1
## 70     160   18     6    1
## 71     160   20     6    1
## 72     157   21     6    1
## 73      41    0     7    1
## 74      49    2     7    1
## 75      57    4     7    1
## 76      71    6     7    1
## 77      89    8     7    1
## 78     112   10     7    1
## 79     146   12     7    1
## 80     174   14     7    1
## 81     218   16     7    1
## 82     250   18     7    1
## 83     288   20     7    1
## 84     305   21     7    1
## 85      42    0     8    1
## 86      50    2     8    1
## 87      61    4     8    1
## 88      71    6     8    1
## 89      84    8     8    1
## 90      93   10     8    1
## 91     110   12     8    1
## 92     116   14     8    1
## 93     126   16     8    1
## 94     134   18     8    1
## 95     125   20     8    1
## 96      42    0     9    1
## 97      51    2     9    1
## 98      59    4     9    1
## 99      68    6     9    1
## 100     85    8     9    1
## 101     96   10     9    1
## 102     90   12     9    1
## 103     92   14     9    1
## 104     93   16     9    1
## 105    100   18     9    1
## 106    100   20     9    1
## 107     98   21     9    1
## 108     41    0    10    1
## 109     44    2    10    1
## 110     52    4    10    1
## 111     63    6    10    1
## 112     74    8    10    1
## 113     81   10    10    1
## 114     89   12    10    1
## 115     96   14    10    1
## 116    101   16    10    1
## 117    112   18    10    1
## 118    120   20    10    1
## 119    124   21    10    1
## 120     43    0    11    1
## 121     51    2    11    1
## 122     63    4    11    1
## 123     84    6    11    1
## 124    112    8    11    1
## 125    139   10    11    1
## 126    168   12    11    1
## 127    177   14    11    1
## 128    182   16    11    1
## 129    184   18    11    1
## 130    181   20    11    1
## 131    175   21    11    1
## 132     41    0    12    1
## 133     49    2    12    1
## 134     56    4    12    1
## 135     62    6    12    1
## 136     72    8    12    1
## 137     88   10    12    1
## 138    119   12    12    1
## 139    135   14    12    1
## 140    162   16    12    1
## 141    185   18    12    1
## 142    195   20    12    1
## 143    205   21    12    1
## 144     41    0    13    1
## 145     48    2    13    1
## 146     53    4    13    1
## 147     60    6    13    1
## 148     65    8    13    1
## 149     67   10    13    1
## 150     71   12    13    1
## 151     70   14    13    1
## 152     71   16    13    1
## 153     81   18    13    1
## 154     91   20    13    1
## 155     96   21    13    1
## 156     41    0    14    1
## 157     49    2    14    1
## 158     62    4    14    1
## 159     79    6    14    1
## 160    101    8    14    1
## 161    128   10    14    1
## 162    164   12    14    1
## 163    192   14    14    1
## 164    227   16    14    1
## 165    248   18    14    1
## 166    259   20    14    1
## 167    266   21    14    1
## 168     41    0    15    1
## 169     49    2    15    1
## 170     56    4    15    1
## 171     64    6    15    1
## 172     68    8    15    1
## 173     68   10    15    1
## 174     67   12    15    1
## 175     68   14    15    1
## 176     41    0    16    1
## 177     45    2    16    1
## 178     49    4    16    1
## 179     51    6    16    1
## 180     57    8    16    1
## 181     51   10    16    1
## 182     54   12    16    1
## 183     42    0    17    1
## 184     51    2    17    1
## 185     61    4    17    1
## 186     72    6    17    1
## 187     83    8    17    1
## 188     89   10    17    1
## 189     98   12    17    1
## 190    103   14    17    1
## 191    113   16    17    1
## 192    123   18    17    1
## 193    133   20    17    1
## 194    142   21    17    1
## 195     39    0    18    1
## 196     35    2    18    1
## 197     43    0    19    1
## 198     48    2    19    1
## 199     55    4    19    1
## 200     62    6    19    1
## 201     65    8    19    1
## 202     71   10    19    1
## 203     82   12    19    1
## 204     88   14    19    1
## 205    106   16    19    1
## 206    120   18    19    1
## 207    144   20    19    1
## 208    157   21    19    1
## 209     41    0    20    1
## 210     47    2    20    1
## 211     54    4    20    1
## 212     58    6    20    1
## 213     65    8    20    1
## 214     73   10    20    1
## 215     77   12    20    1
## 216     89   14    20    1
## 217     98   16    20    1
## 218    107   18    20    1
## 219    115   20    20    1
## 220    117   21    20    1
## 221     40    0    21    2
## 222     50    2    21    2
## 223     62    4    21    2
## 224     86    6    21    2
## 225    125    8    21    2
## 226    163   10    21    2
## 227    217   12    21    2
## 228    240   14    21    2
## 229    275   16    21    2
## 230    307   18    21    2
## 231    318   20    21    2
## 232    331   21    21    2
## 233     41    0    22    2
## 234     55    2    22    2
## 235     64    4    22    2
## 236     77    6    22    2
## 237     90    8    22    2
## 238     95   10    22    2
## 239    108   12    22    2
## 240    111   14    22    2
## 241    131   16    22    2
## 242    148   18    22    2
## 243    164   20    22    2
## 244    167   21    22    2
## 245     43    0    23    2
## 246     52    2    23    2
## 247     61    4    23    2
## 248     73    6    23    2
## 249     90    8    23    2
## 250    103   10    23    2
## 251    127   12    23    2
## 252    135   14    23    2
## 253    145   16    23    2
## 254    163   18    23    2
## 255    170   20    23    2
## 256    175   21    23    2
## 257     42    0    24    2
## 258     52    2    24    2
## 259     58    4    24    2
## 260     74    6    24    2
## 261     66    8    24    2
## 262     68   10    24    2
## 263     70   12    24    2
## 264     71   14    24    2
## 265     72   16    24    2
## 266     72   18    24    2
## 267     76   20    24    2
## 268     74   21    24    2
## 269     40    0    25    2
## 270     49    2    25    2
## 271     62    4    25    2
## 272     78    6    25    2
## 273    102    8    25    2
## 274    124   10    25    2
## 275    146   12    25    2
## 276    164   14    25    2
## 277    197   16    25    2
## 278    231   18    25    2
## 279    259   20    25    2
## 280    265   21    25    2
## 281     42    0    26    2
## 282     48    2    26    2
## 283     57    4    26    2
## 284     74    6    26    2
## 285     93    8    26    2
## 286    114   10    26    2
## 287    136   12    26    2
## 288    147   14    26    2
## 289    169   16    26    2
## 290    205   18    26    2
## 291    236   20    26    2
## 292    251   21    26    2
## 293     39    0    27    2
## 294     46    2    27    2
## 295     58    4    27    2
## 296     73    6    27    2
## 297     87    8    27    2
## 298    100   10    27    2
## 299    115   12    27    2
## 300    123   14    27    2
## 301    144   16    27    2
## 302    163   18    27    2
## 303    185   20    27    2
## 304    192   21    27    2
## 305     39    0    28    2
## 306     46    2    28    2
## 307     58    4    28    2
## 308     73    6    28    2
## 309     92    8    28    2
## 310    114   10    28    2
## 311    145   12    28    2
## 312    156   14    28    2
## 313    184   16    28    2
## 314    207   18    28    2
## 315    212   20    28    2
## 316    233   21    28    2
## 317     39    0    29    2
## 318     48    2    29    2
## 319     59    4    29    2
## 320     74    6    29    2
## 321     87    8    29    2
## 322    106   10    29    2
## 323    134   12    29    2
## 324    150   14    29    2
## 325    187   16    29    2
## 326    230   18    29    2
## 327    279   20    29    2
## 328    309   21    29    2
## 329     42    0    30    2
## 330     48    2    30    2
## 331     59    4    30    2
## 332     72    6    30    2
## 333     85    8    30    2
## 334     98   10    30    2
## 335    115   12    30    2
## 336    122   14    30    2
## 337    143   16    30    2
## 338    151   18    30    2
## 339    157   20    30    2
## 340    150   21    30    2
## 341     42    0    31    3
## 342     53    2    31    3
## 343     62    4    31    3
## 344     73    6    31    3
## 345     85    8    31    3
## 346    102   10    31    3
## 347    123   12    31    3
## 348    138   14    31    3
## 349    170   16    31    3
## 350    204   18    31    3
## 351    235   20    31    3
## 352    256   21    31    3
## 353     41    0    32    3
## 354     49    2    32    3
## 355     65    4    32    3
## 356     82    6    32    3
## 357    107    8    32    3
## 358    129   10    32    3
## 359    159   12    32    3
## 360    179   14    32    3
## 361    221   16    32    3
## 362    263   18    32    3
## 363    291   20    32    3
## 364    305   21    32    3
## 365     39    0    33    3
## 366     50    2    33    3
## 367     63    4    33    3
## 368     77    6    33    3
## 369     96    8    33    3
## 370    111   10    33    3
## 371    137   12    33    3
## 372    144   14    33    3
## 373    151   16    33    3
## 374    146   18    33    3
## 375    156   20    33    3
## 376    147   21    33    3
## 377     41    0    34    3
## 378     49    2    34    3
## 379     63    4    34    3
## 380     85    6    34    3
## 381    107    8    34    3
## 382    134   10    34    3
## 383    164   12    34    3
## 384    186   14    34    3
## 385    235   16    34    3
## 386    294   18    34    3
## 387    327   20    34    3
## 388    341   21    34    3
## 389     41    0    35    3
## 390     53    2    35    3
## 391     64    4    35    3
## 392     87    6    35    3
## 393    123    8    35    3
## 394    158   10    35    3
## 395    201   12    35    3
## 396    238   14    35    3
## 397    287   16    35    3
## 398    332   18    35    3
## 399    361   20    35    3
## 400    373   21    35    3
## 401     39    0    36    3
## 402     48    2    36    3
## 403     61    4    36    3
## 404     76    6    36    3
## 405     98    8    36    3
## 406    116   10    36    3
## 407    145   12    36    3
## 408    166   14    36    3
## 409    198   16    36    3
## 410    227   18    36    3
## 411    225   20    36    3
## 412    220   21    36    3
## 413     41    0    37    3
## 414     48    2    37    3
## 415     56    4    37    3
## 416     68    6    37    3
## 417     80    8    37    3
## 418     83   10    37    3
## 419    103   12    37    3
## 420    112   14    37    3
## 421    135   16    37    3
## 422    157   18    37    3
## 423    169   20    37    3
## 424    178   21    37    3
## 425     41    0    38    3
## 426     49    2    38    3
## 427     61    4    38    3
## 428     74    6    38    3
## 429     98    8    38    3
## 430    109   10    38    3
## 431    128   12    38    3
## 432    154   14    38    3
## 433    192   16    38    3
## 434    232   18    38    3
## 435    280   20    38    3
## 436    290   21    38    3
## 437     42    0    39    3
## 438     50    2    39    3
## 439     61    4    39    3
## 440     78    6    39    3
## 441     89    8    39    3
## 442    109   10    39    3
## 443    130   12    39    3
## 444    146   14    39    3
## 445    170   16    39    3
## 446    214   18    39    3
## 447    250   20    39    3
## 448    272   21    39    3
## 449     41    0    40    3
## 450     55    2    40    3
## 451     66    4    40    3
## 452     79    6    40    3
## 453    101    8    40    3
## 454    120   10    40    3
## 455    154   12    40    3
## 456    182   14    40    3
## 457    215   16    40    3
## 458    262   18    40    3
## 459    295   20    40    3
## 460    321   21    40    3
## 461     42    0    41    4
## 462     51    2    41    4
## 463     66    4    41    4
## 464     85    6    41    4
## 465    103    8    41    4
## 466    124   10    41    4
## 467    155   12    41    4
## 468    153   14    41    4
## 469    175   16    41    4
## 470    184   18    41    4
## 471    199   20    41    4
## 472    204   21    41    4
## 473     42    0    42    4
## 474     49    2    42    4
## 475     63    4    42    4
## 476     84    6    42    4
## 477    103    8    42    4
## 478    126   10    42    4
## 479    160   12    42    4
## 480    174   14    42    4
## 481    204   16    42    4
## 482    234   18    42    4
## 483    269   20    42    4
## 484    281   21    42    4
## 485     42    0    43    4
## 486     55    2    43    4
## 487     69    4    43    4
## 488     96    6    43    4
## 489    131    8    43    4
## 490    157   10    43    4
## 491    184   12    43    4
## 492    188   14    43    4
## 493    197   16    43    4
## 494    198   18    43    4
## 495    199   20    43    4
## 496    200   21    43    4
## 497     42    0    44    4
## 498     51    2    44    4
## 499     65    4    44    4
## 500     86    6    44    4
## 501    103    8    44    4
## 502    118   10    44    4
## 503    127   12    44    4
## 504    138   14    44    4
## 505    145   16    44    4
## 506    146   18    44    4
## 507     41    0    45    4
## 508     50    2    45    4
## 509     61    4    45    4
## 510     78    6    45    4
## 511     98    8    45    4
## 512    117   10    45    4
## 513    135   12    45    4
## 514    141   14    45    4
## 515    147   16    45    4
## 516    174   18    45    4
## 517    197   20    45    4
## 518    196   21    45    4
## 519     40    0    46    4
## 520     52    2    46    4
## 521     62    4    46    4
## 522     82    6    46    4
## 523    101    8    46    4
## 524    120   10    46    4
## 525    144   12    46    4
## 526    156   14    46    4
## 527    173   16    46    4
## 528    210   18    46    4
## 529    231   20    46    4
## 530    238   21    46    4
## 531     41    0    47    4
## 532     53    2    47    4
## 533     66    4    47    4
## 534     79    6    47    4
## 535    100    8    47    4
## 536    123   10    47    4
## 537    148   12    47    4
## 538    157   14    47    4
## 539    168   16    47    4
## 540    185   18    47    4
## 541    210   20    47    4
## 542    205   21    47    4
## 543     39    0    48    4
## 544     50    2    48    4
## 545     62    4    48    4
## 546     80    6    48    4
## 547    104    8    48    4
## 548    125   10    48    4
## 549    154   12    48    4
## 550    170   14    48    4
## 551    222   16    48    4
## 552    261   18    48    4
## 553    303   20    48    4
## 554    322   21    48    4
## 555     40    0    49    4
## 556     53    2    49    4
## 557     64    4    49    4
## 558     85    6    49    4
## 559    108    8    49    4
## 560    128   10    49    4
## 561    152   12    49    4
## 562    166   14    49    4
## 563    184   16    49    4
## 564    203   18    49    4
## 565    233   20    49    4
## 566    237   21    49    4
## 567     41    0    50    4
## 568     54    2    50    4
## 569     67    4    50    4
## 570     84    6    50    4
## 571    105    8    50    4
## 572    122   10    50    4
## 573    155   12    50    4
## 574    175   14    50    4
## 575    205   16    50    4
## 576    234   18    50    4
## 577    264   20    50    4
## 578    264   21    50    4
# Menggunakan data bawaan R
data(ChickWeight)

# Membuat Line Chart
ggplot(data = ChickWeight, aes(x = Time, y = weight, color = Diet, group = Chick)) +
  geom_line(alpha = 0.5) + 
  geom_point(size = 0.5) +  
  labs(title = "Tren Berat Anak Ayam Berdasarkan Waktu",
       x = "Waktu (Hari)",
       y = "Berat (gram)") +
  theme_minimal()

# NO 11
# Load the dataset
data(diamonds)
data
## function (..., list = character(), package = NULL, lib.loc = NULL, 
##     verbose = getOption("verbose"), envir = .GlobalEnv, overwrite = TRUE) 
## {
##     fileExt <- function(x) {
##         db <- grepl("\\.[^.]+\\.(gz|bz2|xz)$", x)
##         ans <- sub(".*\\.", "", x)
##         ans[db] <- sub(".*\\.([^.]+\\.)(gz|bz2|xz)$", "\\1\\2", 
##             x[db])
##         ans
##     }
##     my_read_table <- function(...) {
##         lcc <- Sys.getlocale("LC_COLLATE")
##         on.exit(Sys.setlocale("LC_COLLATE", lcc))
##         Sys.setlocale("LC_COLLATE", "C")
##         read.table(...)
##     }
##     stopifnot(is.character(list))
##     names <- c(as.character(substitute(list(...))[-1L]), list)
##     if (!is.null(package)) {
##         if (!is.character(package)) 
##             stop("'package' must be a character vector or NULL")
##     }
##     paths <- find.package(package, lib.loc, verbose = verbose)
##     if (is.null(lib.loc)) 
##         paths <- c(path.package(package, TRUE), if (!length(package)) getwd(), 
##             paths)
##     paths <- unique(normalizePath(paths[file.exists(paths)]))
##     paths <- paths[dir.exists(file.path(paths, "data"))]
##     dataExts <- tools:::.make_file_exts("data")
##     if (length(names) == 0L) {
##         db <- matrix(character(), nrow = 0L, ncol = 4L)
##         for (path in paths) {
##             entries <- NULL
##             packageName <- if (file_test("-f", file.path(path, 
##                 "DESCRIPTION"))) 
##                 basename(path)
##             else "."
##             if (file_test("-f", INDEX <- file.path(path, "Meta", 
##                 "data.rds"))) {
##                 entries <- readRDS(INDEX)
##             }
##             else {
##                 dataDir <- file.path(path, "data")
##                 entries <- tools::list_files_with_type(dataDir, 
##                   "data")
##                 if (length(entries)) {
##                   entries <- unique(tools::file_path_sans_ext(basename(entries)))
##                   entries <- cbind(entries, "")
##                 }
##             }
##             if (NROW(entries)) {
##                 if (is.matrix(entries) && ncol(entries) == 2L) 
##                   db <- rbind(db, cbind(packageName, dirname(path), 
##                     entries))
##                 else warning(gettextf("data index for package %s is invalid and will be ignored", 
##                   sQuote(packageName)), domain = NA, call. = FALSE)
##             }
##         }
##         colnames(db) <- c("Package", "LibPath", "Item", "Title")
##         footer <- if (missing(package)) 
##             paste0("Use ", sQuote(paste("data(package =", ".packages(all.available = TRUE))")), 
##                 "\n", "to list the data sets in all *available* packages.")
##         else NULL
##         y <- list(title = "Data sets", header = NULL, results = db, 
##             footer = footer)
##         class(y) <- "packageIQR"
##         return(y)
##     }
##     paths <- file.path(paths, "data")
##     for (name in names) {
##         found <- FALSE
##         for (p in paths) {
##             tmp_env <- if (overwrite) 
##                 envir
##             else new.env()
##             if (file_test("-f", file.path(p, "Rdata.rds"))) {
##                 rds <- readRDS(file.path(p, "Rdata.rds"))
##                 if (name %in% names(rds)) {
##                   found <- TRUE
##                   if (verbose) 
##                     message(sprintf("name=%s:\t found in Rdata.rds", 
##                       name), domain = NA)
##                   objs <- rds[[name]]
##                   lazyLoad(file.path(p, "Rdata"), envir = tmp_env, 
##                     filter = function(x) x %in% objs)
##                   break
##                 }
##                 else if (verbose) 
##                   message(sprintf("name=%s:\t NOT found in names() of Rdata.rds, i.e.,\n\t%s\n", 
##                     name, paste(names(rds), collapse = ",")), 
##                     domain = NA)
##             }
##             files <- list.files(p, full.names = TRUE)
##             files <- files[grep(name, files, fixed = TRUE)]
##             if (length(files) > 1L) {
##                 o <- match(fileExt(files), dataExts, nomatch = 100L)
##                 paths0 <- dirname(files)
##                 paths0 <- factor(paths0, levels = unique(paths0))
##                 files <- files[order(paths0, o)]
##             }
##             if (length(files)) {
##                 for (file in files) {
##                   if (verbose) 
##                     message("name=", name, ":\t file= ...", .Platform$file.sep, 
##                       basename(file), "::\t", appendLF = FALSE, 
##                       domain = NA)
##                   ext <- fileExt(file)
##                   if (basename(file) != paste0(name, ".", ext)) 
##                     found <- FALSE
##                   else {
##                     found <- TRUE
##                     switch(ext, R = , r = {
##                       library("utils")
##                       sys.source(file, chdir = TRUE, envir = tmp_env)
##                     }, RData = , rdata = , rda = load(file, envir = tmp_env), 
##                       TXT = , txt = , tab = , tab.gz = , tab.bz2 = , 
##                       tab.xz = , txt.gz = , txt.bz2 = , txt.xz = assign(name, 
##                         my_read_table(file, header = TRUE, as.is = FALSE), 
##                         envir = tmp_env), CSV = , csv = , csv.gz = , 
##                       csv.bz2 = , csv.xz = assign(name, my_read_table(file, 
##                         header = TRUE, sep = ";", as.is = FALSE), 
##                         envir = tmp_env), found <- FALSE)
##                   }
##                   if (found) 
##                     break
##                 }
##                 if (verbose) 
##                   message(if (!found) 
##                     "*NOT* ", "found", domain = NA)
##             }
##             if (found) 
##                 break
##         }
##         if (!found) {
##             warning(gettextf("data set %s not found", sQuote(name)), 
##                 domain = NA)
##         }
##         else if (!overwrite) {
##             for (o in ls(envir = tmp_env, all.names = TRUE)) {
##                 if (exists(o, envir = envir, inherits = FALSE)) 
##                   warning(gettextf("an object named %s already exists and will not be overwritten", 
##                     sQuote(o)))
##                 else assign(o, get(o, envir = tmp_env, inherits = FALSE), 
##                   envir = envir)
##             }
##             rm(tmp_env)
##         }
##     }
##     invisible(names)
## }
## <bytecode: 0x0000021dd9145f58>
## <environment: namespace:utils>
# Membuat Boxplot
ggplot(data = diamonds, aes(x = cut, y = price, fill = cut)) +
  geom_boxplot() +
  labs(title = "Distribusi Harga Berlian berdasarkan Potongan (Cut)",
       x = "Kualitas Potongan (Cut)",
       y = "Harga (Price in USD)") +
  theme_minimal()

# NO 14
# Load the dataset
data1 <- datasets::airquality
# Mengganti missing values dengan median di setiap kolom
airquality$Ozone[is.na(airquality$Ozone)] <- median(airquality$Ozone, na.rm = TRUE)

airquality
##     Ozone Solar.R Wind Temp Month Day
## 1    41.0     190  7.4   67     5   1
## 2    36.0     118  8.0   72     5   2
## 3    12.0     149 12.6   74     5   3
## 4    18.0     313 11.5   62     5   4
## 5    31.5      NA 14.3   56     5   5
## 6    28.0      NA 14.9   66     5   6
## 7    23.0     299  8.6   65     5   7
## 8    19.0      99 13.8   59     5   8
## 9     8.0      19 20.1   61     5   9
## 10   31.5     194  8.6   69     5  10
## 11    7.0      NA  6.9   74     5  11
## 12   16.0     256  9.7   69     5  12
## 13   11.0     290  9.2   66     5  13
## 14   14.0     274 10.9   68     5  14
## 15   18.0      65 13.2   58     5  15
## 16   14.0     334 11.5   64     5  16
## 17   34.0     307 12.0   66     5  17
## 18    6.0      78 18.4   57     5  18
## 19   30.0     322 11.5   68     5  19
## 20   11.0      44  9.7   62     5  20
## 21    1.0       8  9.7   59     5  21
## 22   11.0     320 16.6   73     5  22
## 23    4.0      25  9.7   61     5  23
## 24   32.0      92 12.0   61     5  24
## 25   31.5      66 16.6   57     5  25
## 26   31.5     266 14.9   58     5  26
## 27   31.5      NA  8.0   57     5  27
## 28   23.0      13 12.0   67     5  28
## 29   45.0     252 14.9   81     5  29
## 30  115.0     223  5.7   79     5  30
## 31   37.0     279  7.4   76     5  31
## 32   31.5     286  8.6   78     6   1
## 33   31.5     287  9.7   74     6   2
## 34   31.5     242 16.1   67     6   3
## 35   31.5     186  9.2   84     6   4
## 36   31.5     220  8.6   85     6   5
## 37   31.5     264 14.3   79     6   6
## 38   29.0     127  9.7   82     6   7
## 39   31.5     273  6.9   87     6   8
## 40   71.0     291 13.8   90     6   9
## 41   39.0     323 11.5   87     6  10
## 42   31.5     259 10.9   93     6  11
## 43   31.5     250  9.2   92     6  12
## 44   23.0     148  8.0   82     6  13
## 45   31.5     332 13.8   80     6  14
## 46   31.5     322 11.5   79     6  15
## 47   21.0     191 14.9   77     6  16
## 48   37.0     284 20.7   72     6  17
## 49   20.0      37  9.2   65     6  18
## 50   12.0     120 11.5   73     6  19
## 51   13.0     137 10.3   76     6  20
## 52   31.5     150  6.3   77     6  21
## 53   31.5      59  1.7   76     6  22
## 54   31.5      91  4.6   76     6  23
## 55   31.5     250  6.3   76     6  24
## 56   31.5     135  8.0   75     6  25
## 57   31.5     127  8.0   78     6  26
## 58   31.5      47 10.3   73     6  27
## 59   31.5      98 11.5   80     6  28
## 60   31.5      31 14.9   77     6  29
## 61   31.5     138  8.0   83     6  30
## 62  135.0     269  4.1   84     7   1
## 63   49.0     248  9.2   85     7   2
## 64   32.0     236  9.2   81     7   3
## 65   31.5     101 10.9   84     7   4
## 66   64.0     175  4.6   83     7   5
## 67   40.0     314 10.9   83     7   6
## 68   77.0     276  5.1   88     7   7
## 69   97.0     267  6.3   92     7   8
## 70   97.0     272  5.7   92     7   9
## 71   85.0     175  7.4   89     7  10
## 72   31.5     139  8.6   82     7  11
## 73   10.0     264 14.3   73     7  12
## 74   27.0     175 14.9   81     7  13
## 75   31.5     291 14.9   91     7  14
## 76    7.0      48 14.3   80     7  15
## 77   48.0     260  6.9   81     7  16
## 78   35.0     274 10.3   82     7  17
## 79   61.0     285  6.3   84     7  18
## 80   79.0     187  5.1   87     7  19
## 81   63.0     220 11.5   85     7  20
## 82   16.0       7  6.9   74     7  21
## 83   31.5     258  9.7   81     7  22
## 84   31.5     295 11.5   82     7  23
## 85   80.0     294  8.6   86     7  24
## 86  108.0     223  8.0   85     7  25
## 87   20.0      81  8.6   82     7  26
## 88   52.0      82 12.0   86     7  27
## 89   82.0     213  7.4   88     7  28
## 90   50.0     275  7.4   86     7  29
## 91   64.0     253  7.4   83     7  30
## 92   59.0     254  9.2   81     7  31
## 93   39.0      83  6.9   81     8   1
## 94    9.0      24 13.8   81     8   2
## 95   16.0      77  7.4   82     8   3
## 96   78.0      NA  6.9   86     8   4
## 97   35.0      NA  7.4   85     8   5
## 98   66.0      NA  4.6   87     8   6
## 99  122.0     255  4.0   89     8   7
## 100  89.0     229 10.3   90     8   8
## 101 110.0     207  8.0   90     8   9
## 102  31.5     222  8.6   92     8  10
## 103  31.5     137 11.5   86     8  11
## 104  44.0     192 11.5   86     8  12
## 105  28.0     273 11.5   82     8  13
## 106  65.0     157  9.7   80     8  14
## 107  31.5      64 11.5   79     8  15
## 108  22.0      71 10.3   77     8  16
## 109  59.0      51  6.3   79     8  17
## 110  23.0     115  7.4   76     8  18
## 111  31.0     244 10.9   78     8  19
## 112  44.0     190 10.3   78     8  20
## 113  21.0     259 15.5   77     8  21
## 114   9.0      36 14.3   72     8  22
## 115  31.5     255 12.6   75     8  23
## 116  45.0     212  9.7   79     8  24
## 117 168.0     238  3.4   81     8  25
## 118  73.0     215  8.0   86     8  26
## 119  31.5     153  5.7   88     8  27
## 120  76.0     203  9.7   97     8  28
## 121 118.0     225  2.3   94     8  29
## 122  84.0     237  6.3   96     8  30
## 123  85.0     188  6.3   94     8  31
## 124  96.0     167  6.9   91     9   1
## 125  78.0     197  5.1   92     9   2
## 126  73.0     183  2.8   93     9   3
## 127  91.0     189  4.6   93     9   4
## 128  47.0      95  7.4   87     9   5
## 129  32.0      92 15.5   84     9   6
## 130  20.0     252 10.9   80     9   7
## 131  23.0     220 10.3   78     9   8
## 132  21.0     230 10.9   75     9   9
## 133  24.0     259  9.7   73     9  10
## 134  44.0     236 14.9   81     9  11
## 135  21.0     259 15.5   76     9  12
## 136  28.0     238  6.3   77     9  13
## 137   9.0      24 10.9   71     9  14
## 138  13.0     112 11.5   71     9  15
## 139  46.0     237  6.9   78     9  16
## 140  18.0     224 13.8   67     9  17
## 141  13.0      27 10.3   76     9  18
## 142  24.0     238 10.3   68     9  19
## 143  16.0     201  8.0   82     9  20
## 144  13.0     238 12.6   64     9  21
## 145  23.0      14  9.2   71     9  22
## 146  36.0     139 10.3   81     9  23
## 147   7.0      49 10.3   69     9  24
## 148  14.0      20 16.6   63     9  25
## 149  30.0     193  6.9   70     9  26
## 150  31.5     145 13.2   77     9  27
## 151  14.0     191 14.3   75     9  28
## 152  18.0     131  8.0   76     9  29
## 153  20.0     223 11.5   68     9  30
colSums(is.na(airquality))
##   Ozone Solar.R    Wind    Temp   Month     Day 
##       0       7       0       0       0       0
# NO 15
# Load the dataset
data1 <- datasets::mtcars
# Melihat data 6 baris pertama
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
# Menghitung korelasi antara mpg dan cyl
cor(mtcars$mpg, mtcars$cyl)
## [1] -0.852162
# NO 18
# Load the dataset
plot(mtcars$wt, mtcars$mpg,
     main = "Scatterplot MPG vs WT",
     xlab = "Weight (1000 lbs)",
     ylab = "Miles per Gallon",
     pch = 19, col = "blue")

# NO 21
data(diamonds)
data
## function (..., list = character(), package = NULL, lib.loc = NULL, 
##     verbose = getOption("verbose"), envir = .GlobalEnv, overwrite = TRUE) 
## {
##     fileExt <- function(x) {
##         db <- grepl("\\.[^.]+\\.(gz|bz2|xz)$", x)
##         ans <- sub(".*\\.", "", x)
##         ans[db] <- sub(".*\\.([^.]+\\.)(gz|bz2|xz)$", "\\1\\2", 
##             x[db])
##         ans
##     }
##     my_read_table <- function(...) {
##         lcc <- Sys.getlocale("LC_COLLATE")
##         on.exit(Sys.setlocale("LC_COLLATE", lcc))
##         Sys.setlocale("LC_COLLATE", "C")
##         read.table(...)
##     }
##     stopifnot(is.character(list))
##     names <- c(as.character(substitute(list(...))[-1L]), list)
##     if (!is.null(package)) {
##         if (!is.character(package)) 
##             stop("'package' must be a character vector or NULL")
##     }
##     paths <- find.package(package, lib.loc, verbose = verbose)
##     if (is.null(lib.loc)) 
##         paths <- c(path.package(package, TRUE), if (!length(package)) getwd(), 
##             paths)
##     paths <- unique(normalizePath(paths[file.exists(paths)]))
##     paths <- paths[dir.exists(file.path(paths, "data"))]
##     dataExts <- tools:::.make_file_exts("data")
##     if (length(names) == 0L) {
##         db <- matrix(character(), nrow = 0L, ncol = 4L)
##         for (path in paths) {
##             entries <- NULL
##             packageName <- if (file_test("-f", file.path(path, 
##                 "DESCRIPTION"))) 
##                 basename(path)
##             else "."
##             if (file_test("-f", INDEX <- file.path(path, "Meta", 
##                 "data.rds"))) {
##                 entries <- readRDS(INDEX)
##             }
##             else {
##                 dataDir <- file.path(path, "data")
##                 entries <- tools::list_files_with_type(dataDir, 
##                   "data")
##                 if (length(entries)) {
##                   entries <- unique(tools::file_path_sans_ext(basename(entries)))
##                   entries <- cbind(entries, "")
##                 }
##             }
##             if (NROW(entries)) {
##                 if (is.matrix(entries) && ncol(entries) == 2L) 
##                   db <- rbind(db, cbind(packageName, dirname(path), 
##                     entries))
##                 else warning(gettextf("data index for package %s is invalid and will be ignored", 
##                   sQuote(packageName)), domain = NA, call. = FALSE)
##             }
##         }
##         colnames(db) <- c("Package", "LibPath", "Item", "Title")
##         footer <- if (missing(package)) 
##             paste0("Use ", sQuote(paste("data(package =", ".packages(all.available = TRUE))")), 
##                 "\n", "to list the data sets in all *available* packages.")
##         else NULL
##         y <- list(title = "Data sets", header = NULL, results = db, 
##             footer = footer)
##         class(y) <- "packageIQR"
##         return(y)
##     }
##     paths <- file.path(paths, "data")
##     for (name in names) {
##         found <- FALSE
##         for (p in paths) {
##             tmp_env <- if (overwrite) 
##                 envir
##             else new.env()
##             if (file_test("-f", file.path(p, "Rdata.rds"))) {
##                 rds <- readRDS(file.path(p, "Rdata.rds"))
##                 if (name %in% names(rds)) {
##                   found <- TRUE
##                   if (verbose) 
##                     message(sprintf("name=%s:\t found in Rdata.rds", 
##                       name), domain = NA)
##                   objs <- rds[[name]]
##                   lazyLoad(file.path(p, "Rdata"), envir = tmp_env, 
##                     filter = function(x) x %in% objs)
##                   break
##                 }
##                 else if (verbose) 
##                   message(sprintf("name=%s:\t NOT found in names() of Rdata.rds, i.e.,\n\t%s\n", 
##                     name, paste(names(rds), collapse = ",")), 
##                     domain = NA)
##             }
##             files <- list.files(p, full.names = TRUE)
##             files <- files[grep(name, files, fixed = TRUE)]
##             if (length(files) > 1L) {
##                 o <- match(fileExt(files), dataExts, nomatch = 100L)
##                 paths0 <- dirname(files)
##                 paths0 <- factor(paths0, levels = unique(paths0))
##                 files <- files[order(paths0, o)]
##             }
##             if (length(files)) {
##                 for (file in files) {
##                   if (verbose) 
##                     message("name=", name, ":\t file= ...", .Platform$file.sep, 
##                       basename(file), "::\t", appendLF = FALSE, 
##                       domain = NA)
##                   ext <- fileExt(file)
##                   if (basename(file) != paste0(name, ".", ext)) 
##                     found <- FALSE
##                   else {
##                     found <- TRUE
##                     switch(ext, R = , r = {
##                       library("utils")
##                       sys.source(file, chdir = TRUE, envir = tmp_env)
##                     }, RData = , rdata = , rda = load(file, envir = tmp_env), 
##                       TXT = , txt = , tab = , tab.gz = , tab.bz2 = , 
##                       tab.xz = , txt.gz = , txt.bz2 = , txt.xz = assign(name, 
##                         my_read_table(file, header = TRUE, as.is = FALSE), 
##                         envir = tmp_env), CSV = , csv = , csv.gz = , 
##                       csv.bz2 = , csv.xz = assign(name, my_read_table(file, 
##                         header = TRUE, sep = ";", as.is = FALSE), 
##                         envir = tmp_env), found <- FALSE)
##                   }
##                   if (found) 
##                     break
##                 }
##                 if (verbose) 
##                   message(if (!found) 
##                     "*NOT* ", "found", domain = NA)
##             }
##             if (found) 
##                 break
##         }
##         if (!found) {
##             warning(gettextf("data set %s not found", sQuote(name)), 
##                 domain = NA)
##         }
##         else if (!overwrite) {
##             for (o in ls(envir = tmp_env, all.names = TRUE)) {
##                 if (exists(o, envir = envir, inherits = FALSE)) 
##                   warning(gettextf("an object named %s already exists and will not be overwritten", 
##                     sQuote(o)))
##                 else assign(o, get(o, envir = tmp_env, inherits = FALSE), 
##                   envir = envir)
##             }
##             rm(tmp_env)
##         }
##     }
##     invisible(names)
## }
## <bytecode: 0x0000021dd9145f58>
## <environment: namespace:utils>
library(ggplot2)
str(diamonds$cut)
##  Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ...
# NO 22
# Memuat data (opsional, faithful sudah ada di base R)
data(faithful)

# Menghitung korelasi Pearson (default)
cor(faithful$eruptions, faithful$waiting)
## [1] 0.9008112
# NO 25
# Memuat dataset Titanic bawaan R
data("Titanic")

# Mengubahnya menjadi dataframe agar bisa menggunakan sintaks $
titanic <- as.data.frame(Titanic)

# Melihat tabel hitungan (0: tidak selamat, 1: selamat)
table(titanic$Survived)
## 
##  No Yes 
##  16  16
# Total jumlah yang selamat saja (menjumlahkan angka 1)
sum(titanic$Survived == 1)
## [1] 0
# NO 28
str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
# NO 29
data(iris)
head(iris) # Melihat 6 baris pertama
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
boxplot(Sepal.Length ~ Species, data = iris,
        main = "Perbedaan Sepal.Length berdasarkan Spesies",
        xlab = "Spesies",
        ylab = "Sepal Length (cm)",
        col = c("red", "green", "blue"))